Add --no_fwd_decls option

As requested in #354.  Marks all uses as full uses.  Useful for
compliance with Google C++ Style Guide.
This commit is contained in:
Alex Kirchhoff 2017-11-21 18:42:38 -08:00 committed by Kim Gräsman
parent c6e4125389
commit 4275af7cec
5 changed files with 45 additions and 3 deletions

View File

@ -92,6 +92,7 @@ static void PrintHelp(const char* extra_msg) {
" the maximum line length can still be exceeded with long\n"
" file names (default: 80).\n"
" --no_comments: do not add 'why' comments.\n"
" --no_fwd_decls: do not use forward declarations.\n"
" --verbose=<level>: the higher the level, the more output.\n"
"\n"
"In addition to IWYU-specific options you can specify the following\n"
@ -160,7 +161,8 @@ CommandlineFlags::CommandlineFlags()
max_line_length(80),
prefix_header_include_policy(CommandlineFlags::kAdd),
pch_in_code(false),
no_comments(false) {
no_comments(false),
no_fwd_decls(false) {
}
int CommandlineFlags::ParseArgv(int argc, char** argv) {
@ -176,6 +178,7 @@ int CommandlineFlags::ParseArgv(int argc, char** argv) {
{"pch_in_code", no_argument, nullptr, 'h'},
{"max_line_length", optional_argument, nullptr, 'l'},
{"no_comments", optional_argument, nullptr, 'o'},
{"no_fwd_decls", optional_argument, nullptr, 'f'},
{nullptr, 0, nullptr, 0}
};
static const char shortopts[] = "d::p:v:c:m:n";
@ -189,6 +192,7 @@ int CommandlineFlags::ParseArgv(int argc, char** argv) {
case 'm': mapping_files.push_back(optarg); break;
case 'n': no_default_mappings = true; break;
case 'o': no_comments = true; break;
case 'f': no_fwd_decls = true; break;
case 'x':
if (strcmp(optarg, "add") == 0) {
prefix_header_include_policy = CommandlineFlags::kAdd;

View File

@ -98,8 +98,9 @@ struct CommandlineFlags {
int max_line_length;
// Policy regarding files included via -include option. No short option.
PrefixHeaderIncludePolicy prefix_header_include_policy;
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 pch_in_code; // Treat the first seen include as a PCH. No short option.
bool no_comments; // Disable 'why' comments. No short option.
bool no_fwd_decls; // Disable forward declarations.
};
const CommandlineFlags& GlobalFlags();

View File

@ -874,6 +874,7 @@ set<string> CalculateMinimalIncludes(
// A6) If any of the redeclarations of this declaration is in the same
// file as the use (and before it), and is actually a definition,
// discard the forward-declare use.
// A7) If --no_fwd_decls has been passed, recategorize as a full use.
// Trimming symbol uses (1st pass):
// B1) If the definition of a full use comes after the use, change the
@ -1032,6 +1033,11 @@ void ProcessForwardDeclare(OneUse* use,
return;
}
}
// (A7) If --no_fwd_decls has been passed, recategorize as a full use.
if (GlobalFlags().no_fwd_decls) {
use->set_full_use();
}
}
void ProcessFullUse(OneUse* use,

View File

@ -76,6 +76,7 @@ class OneIwyuTest(unittest.TestCase):
'--transitive_includes_only'],
'no_h_includes_cc.cc': [self.CheckAlsoExtension('.c')],
'no_comments.cc': ['--no_comments'],
'no_fwd_decls.cc': ['--no_fwd_decls'],
'overloaded_class.cc': [self.CheckAlsoExtension('-i1.h')],
'pch_in_code.cc': ['--pch_in_code', '--prefix_header_includes=remove'],
'prefix_header_attribution.cc': ['--prefix_header_includes=remove'],
@ -154,6 +155,7 @@ class OneIwyuTest(unittest.TestCase):
'new_header_path_provided.cc': ['.'],
'no_comments.cc': ['.'],
'no_fwd_decl_nested_class.cc': ['.'],
'no_fwd_decls.cc': ['.'],
'no_h_includes_cc.cc': ['.'],
'non_transitive_include.cc': ['.'],
'overloaded_class.cc': ['.'],

29
tests/cxx/no_fwd_decls.cc Normal file
View File

@ -0,0 +1,29 @@
//===--- no_fwd_decls.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.
//
//===----------------------------------------------------------------------===//
// Test that passing the --no_fwd_decls switch to IWYU suggests including the
// corresponding header file even when the use is not a full use.
#include "tests/cxx/direct.h"
// IWYU: IndirectClass is...*indirect.h
IndirectClass* global;
/**** IWYU_SUMMARY
tests/cxx/no_fwd_decls.cc should add these lines:
#include "tests/cxx/indirect.h"
tests/cxx/no_fwd_decls.cc should remove these lines:
- #include "tests/cxx/direct.h" // lines XX-XX
The full include-list for tests/cxx/no_fwd_decls.cc:
#include "tests/cxx/indirect.h" // for IndirectClass
***** IWYU_SUMMARY */