diff options
author | Daniel Hannon <hello111f@gmail.com> | 2022-05-27 22:08:32 +0100 |
---|---|---|
committer | Kim Gräsman <kim.grasman@gmail.com> | 2022-05-28 11:03:47 +0200 |
commit | 3f456f66c22767a19477d189deea6fdc169de90b (patch) | |
tree | 593e3176473d2ecf2a13c069b9028a9e0e1c90d6 | |
parent | 67942da4111888b800b2be34df0f7a432621256f (diff) |
add --comment_style option with tests
-rw-r--r-- | include-what-you-use.1 | 18 | ||||
-rw-r--r-- | iwyu_globals.cc | 20 | ||||
-rw-r--r-- | iwyu_globals.h | 1 | ||||
-rw-r--r-- | iwyu_output.cc | 6 | ||||
-rw-r--r-- | tests/cxx/comment_style-d1.h | 16 | ||||
-rw-r--r-- | tests/cxx/comment_style-i2.h | 14 | ||||
-rw-r--r-- | tests/cxx/comment_style_long.cc | 32 | ||||
-rw-r--r-- | tests/cxx/comment_style_none.cc | 32 | ||||
-rw-r--r-- | tests/cxx/comment_style_short.cc | 32 | ||||
-rw-r--r-- | tests/cxx/comment_style_update_long.cc | 34 | ||||
-rw-r--r-- | tests/cxx/comment_style_update_none.cc | 34 |
11 files changed, 238 insertions, 1 deletions
diff --git a/include-what-you-use.1 b/include-what-you-use.1 index 446e88a..3132d92 100644 --- a/include-what-you-use.1 +++ b/include-what-you-use.1 @@ -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 diff --git a/iwyu_globals.cc b/iwyu_globals.cc index 73c8853..8a32bda 100644 --- a/iwyu_globals.cc +++ b/iwyu_globals.cc @@ -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) { diff --git a/iwyu_globals.h b/iwyu_globals.h index 8066890..ea606dc 100644 --- a/iwyu_globals.h +++ b/iwyu_globals.h @@ -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 diff --git a/iwyu_output.cc b/iwyu_output.cc index e16443e..232af4e 100644 --- a/iwyu_output.cc +++ b/iwyu_output.cc @@ -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()); + } } } } diff --git a/tests/cxx/comment_style-d1.h b/tests/cxx/comment_style-d1.h new file mode 100644 index 0000000..a104aca --- /dev/null +++ b/tests/cxx/comment_style-d1.h @@ -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; + } +}; diff --git a/tests/cxx/comment_style-i2.h b/tests/cxx/comment_style-i2.h new file mode 100644 index 0000000..ebc06ba --- /dev/null +++ b/tests/cxx/comment_style-i2.h @@ -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; + } +}; diff --git a/tests/cxx/comment_style_long.cc b/tests/cxx/comment_style_long.cc new file mode 100644 index 0000000..9296067 --- /dev/null +++ b/tests/cxx/comment_style_long.cc @@ -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 */ diff --git a/tests/cxx/comment_style_none.cc b/tests/cxx/comment_style_none.cc new file mode 100644 index 0000000..5e071ec --- /dev/null +++ b/tests/cxx/comment_style_none.cc @@ -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 */ diff --git a/tests/cxx/comment_style_short.cc b/tests/cxx/comment_style_short.cc new file mode 100644 index 0000000..4879fdc --- /dev/null +++ b/tests/cxx/comment_style_short.cc @@ -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 */ diff --git a/tests/cxx/comment_style_update_long.cc b/tests/cxx/comment_style_update_long.cc new file mode 100644 index 0000000..dffe987 --- /dev/null +++ b/tests/cxx/comment_style_update_long.cc @@ -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 */ diff --git a/tests/cxx/comment_style_update_none.cc b/tests/cxx/comment_style_update_none.cc new file mode 100644 index 0000000..df87ea9 --- /dev/null +++ b/tests/cxx/comment_style_update_none.cc @@ -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 */ |