add --comment_style option with tests

This commit is contained in:
Daniel Hannon 2022-05-27 22:08:32 +01:00 committed by Kim Gräsman
parent 67942da411
commit 3f456f66c2
11 changed files with 238 additions and 1 deletions

View File

@ -44,6 +44,24 @@ given glob pattern (in addition to the default of reporting for the input
source file and associated header files).
This flag may be specified multiple times to specify multiple glob patterns.
.TP
.BI \-\-comment_style= verbosity
Controls the style and verbosity of \(lqwhy\(rq comments at the end of
suggested includes. Options for
.I verbosity
are:
.RS
.TP
.B none
No \(lqwhy\(rq comments.
.TP
.B short
\(lqWhy\(rq comments include symbol names, but no namespaces. This is the
default.
.TP
.B long
\(lqWhy\(rq comments include symbol names with namespaces.
.RE
.TP
.B \-\-cxx17ns
Suggest the more concise syntax for nested namespaces introduced in C++17.
.TP

View File

@ -90,6 +90,12 @@ static void PrintHelp(const char* extra_msg) {
" Note that this only affects comments and alignment thereof,\n"
" the maximum line length can still be exceeded with long\n"
" file names (default: 80).\n"
" --comment_style=<level> set verbosity of 'why' comments to one\n"
" of the following values:\n"
" none: do not add 'why' comments\n"
" short: 'why' comments do not include namespaces\n"
" long: 'why' comments include namespaces\n"
" Default value is 'short'.\n"
" --no_comments: do not add 'why' comments.\n"
" --update_comments: update and insert 'why' comments, even if no\n"
" #include lines need to be added or removed.\n"
@ -185,6 +191,7 @@ CommandlineFlags::CommandlineFlags()
pch_in_code(false),
no_comments(false),
update_comments(false),
comments_with_namespace(false),
no_fwd_decls(false),
quoted_includes_first(false),
cxx17ns(false),
@ -205,6 +212,7 @@ int CommandlineFlags::ParseArgv(int argc, char** argv) {
{"prefix_header_includes", required_argument, nullptr, 'x'},
{"pch_in_code", no_argument, nullptr, 'h'},
{"max_line_length", required_argument, nullptr, 'l'},
{"comment_style", required_argument, nullptr, 'i'},
{"no_comments", no_argument, nullptr, 'o'},
{"update_comments", no_argument, nullptr, 'u'},
{"no_fwd_decls", no_argument, nullptr, 'f'},
@ -225,6 +233,18 @@ int CommandlineFlags::ParseArgv(int argc, char** argv) {
case 'n': no_default_mappings = true; break;
case 'o': no_comments = true; break;
case 'u': update_comments = true; break;
case 'i':
if (strcmp(optarg, "none") == 0) {
no_comments = true;
} else if (strcmp(optarg, "short") == 0) {
comments_with_namespace = false;
} else if (strcmp(optarg, "long") == 0) {
comments_with_namespace = true;
} else {
PrintHelp("FATAL ERROR: unknown comment style.");
exit(EXIT_FAILURE);
}
break;
case 'f': no_fwd_decls = true; break;
case 'x':
if (strcmp(optarg, "add") == 0) {

View File

@ -90,6 +90,7 @@ struct CommandlineFlags {
bool pch_in_code; // Treat the first seen include as a PCH. No short option.
bool no_comments; // Disable 'why' comments. No short option.
bool update_comments; // Force 'why' comments. No short option.
bool comments_with_namespace; // Show namespace in 'why' comments.
bool no_fwd_decls; // Disable forward declarations.
bool quoted_includes_first; // Place quoted includes first in sort order.
bool cxx17ns; // -C: C++17 nested namespace syntax

View File

@ -1747,7 +1747,11 @@ void CalculateDesiredIncludesAndForwardDeclares(
auto range = include_map.equal_range(use.suggested_header());
for (auto it = range.first; it != range.second; ++it) {
it->second->set_desired();
it->second->AddSymbolUse(use.short_symbol_name());
if (GlobalFlags().comments_with_namespace) {
it->second->AddSymbolUse(use.symbol_name());
} else {
it->second->AddSymbolUse(use.short_symbol_name());
}
}
}
}

View File

@ -0,0 +1,16 @@
//===--- comment_style-d1.h - test input file for iwyu --------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "tests/cxx/comment_style-i2.h"
namespace Foo {
int bar(int x) {
return x;
}
};

View File

@ -0,0 +1,14 @@
//===--- comment_style-i2.h - test input file for iwyu --------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
namespace Bar {
int foo(int x) {
return x;
}
};

View File

@ -0,0 +1,32 @@
//===--- comment_style_long.cc - test input file for iwyu -----------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// IWYU_ARGS: -Xiwyu --comment_style=long -I .
// Test behavior is right with long comments.
#include "tests/cxx/comment_style-d1.h" // for bar
int main() {
Foo::bar(1);
// IWYU: Bar::foo is...*"tests/cxx/comment_style-i2.h"
Bar::foo(2);
return 0;
}
/**** IWYU_SUMMARY
tests/cxx/comment_style_long.cc should add these lines:
#include "tests/cxx/comment_style-i2.h"
tests/cxx/comment_style_long.cc should remove these lines:
The full include-list for tests/cxx/comment_style_long.cc:
#include "tests/cxx/comment_style-d1.h" // for Foo::bar
#include "tests/cxx/comment_style-i2.h" // for Bar::foo
***** IWYU_SUMMARY */

View File

@ -0,0 +1,32 @@
//===--- comment_style_none.cc - test input file for iwyu -----------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// IWYU_ARGS: -Xiwyu --comment_style=none -I .
// Test that --comment_style=none adds no comments.
#include "tests/cxx/comment_style-d1.h"
int main() {
Foo::bar(1);
// IWYU: Bar::foo is...*"tests/cxx/comment_style-i2.h"
Bar::foo(2);
return 0;
}
/**** IWYU_SUMMARY
tests/cxx/comment_style_none.cc should add these lines:
#include "tests/cxx/comment_style-i2.h"
tests/cxx/comment_style_none.cc should remove these lines:
The full include-list for tests/cxx/comment_style_none.cc:
#include "tests/cxx/comment_style-d1.h"
#include "tests/cxx/comment_style-i2.h"
***** IWYU_SUMMARY */

View File

@ -0,0 +1,32 @@
//===--- comment_style_short.cc - test input file for iwyu ----------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// IWYU_ARGS: -Xiwyu --comment_style=short -I .
// Test that --comment_style=short adds short comments.
#include "tests/cxx/comment_style-d1.h" // some Comment
int main() {
Foo::bar(1);
// IWYU: Bar::foo is...*"tests/cxx/comment_style-i2.h"
Bar::foo(2);
return 0;
}
/**** IWYU_SUMMARY
tests/cxx/comment_style_short.cc should add these lines:
#include "tests/cxx/comment_style-i2.h"
tests/cxx/comment_style_short.cc should remove these lines:
The full include-list for tests/cxx/comment_style_short.cc:
#include "tests/cxx/comment_style-d1.h" // for bar
#include "tests/cxx/comment_style-i2.h" // for foo
***** IWYU_SUMMARY */

View File

@ -0,0 +1,34 @@
//===--- comment_style_update_long.cc - test input file for iwyu ----------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// IWYU_ARGS: -Xiwyu --update_comments -Xiwyu --comment_style=long -I .
// Test that passing --update_comments respects comment style.
#include "tests/cxx/comment_style-d1.h"
int main() {
// IWYU: Bar::foo is...*"tests/cxx/comment_style-i2.h"
Bar::foo(123);
Foo::bar(456);
return 0;
}
/**** IWYU_SUMMARY
tests/cxx/comment_style_update_long.cc should add these lines:
#include "tests/cxx/comment_style-i2.h"
tests/cxx/comment_style_update_long.cc should remove these lines:
The full include-list for tests/cxx/comment_style_update_long.cc:
#include "tests/cxx/comment_style-d1.h" // for Foo::bar
#include "tests/cxx/comment_style-i2.h" // for Bar::foo
***** IWYU_SUMMARY */

View File

@ -0,0 +1,34 @@
//===--- comment_style_update_none.cc - test input file for iwyu ----------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// IWYU_ARGS: -Xiwyu --update_comments -Xiwyu --comment_style=none -I .
// Test that --update_comments respects comment style.
#include "tests/cxx/comment_style-d1.h" // for foo, bar
int main() {
// IWYU: Bar::foo is...*"tests/cxx/comment_style-i2.h"
Bar::foo(123);
Foo::bar(456);
return 0;
}
/**** IWYU_SUMMARY
tests/cxx/comment_style_update_none.cc should add these lines:
#include "tests/cxx/comment_style-i2.h"
tests/cxx/comment_style_update_none.cc should remove these lines:
The full include-list for tests/cxx/comment_style_update_none.cc:
#include "tests/cxx/comment_style-d1.h"
#include "tests/cxx/comment_style-i2.h"
***** IWYU_SUMMARY */