2012-11-25 22:09:37 +00:00
|
|
|
//===--- iwyu_globals.h - global variables for include-what-you-use -------===//
|
2011-02-04 22:28:15 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// The source manager is used in just too many places.
|
|
|
|
|
2016-05-22 09:06:36 +01:00
|
|
|
#ifndef INCLUDE_WHAT_YOU_USE_IWYU_GLOBALS_H_
|
|
|
|
#define INCLUDE_WHAT_YOU_USE_IWYU_GLOBALS_H_
|
2011-02-04 22:28:15 +00:00
|
|
|
|
2011-05-04 19:30:53 +01:00
|
|
|
#include <set> // for set
|
|
|
|
#include <string> // for string
|
|
|
|
#include <vector> // for vector
|
2011-05-04 19:29:59 +01:00
|
|
|
|
2011-03-18 07:00:52 +00:00
|
|
|
namespace clang {
|
2011-05-04 19:29:59 +01:00
|
|
|
class FileEntry;
|
2011-03-18 07:00:52 +00:00
|
|
|
class HeaderSearch;
|
2011-05-04 19:29:59 +01:00
|
|
|
class SourceManager;
|
|
|
|
struct PrintingPolicy;
|
|
|
|
} // namespace clang
|
2011-03-18 07:00:52 +00:00
|
|
|
|
2011-02-04 22:28:15 +00:00
|
|
|
namespace include_what_you_use {
|
|
|
|
|
2011-04-27 00:12:44 +01:00
|
|
|
using std::set;
|
2011-02-04 22:28:15 +00:00
|
|
|
using std::string;
|
2011-03-18 07:00:52 +00:00
|
|
|
using std::vector;
|
2011-02-04 22:28:15 +00:00
|
|
|
|
Add --regex option
As reported in issue #981, using std::regex in IWYU has caused a
tremendous performance regression for large mapping files containing
regex mappings.
$ cat t.cc
#include <string>
# with llvm::Regex
$ time include-what-you-use -Xiwyu --mapping_file=qt5_11.imp t.cc
...
real 0m0,529s
user 0m0,509s
sys 0m0,020s
# with std::regex
$ time include-what-you-use -Xiwyu --mapping_file=qt5_11.imp t.cc
...
real 0m29,870s
user 0m29,717s
sys 0m0,012s
qt5_11.imp contains 2300+ regex mappings, and <string> has a bunch of
includes, so this is a good testbed for regular expression engines, but
over 50x slower is not the result we were hoping for.
The reason we switched to std::regex was to get support for negative
lookaround (llvm::Regex does not have it), but exotic regexes in
mappings are pretty rare, and this is a significant performance hit.
Introduce a --regex option to select regex dialect, with documented
tradeoffs. Put the default back to LLVM's fast implementation.
This fixes issue #981.
2022-09-02 19:55:48 +01:00
|
|
|
enum class RegexDialect;
|
2011-04-06 21:06:56 +01:00
|
|
|
class FullUseCache;
|
2011-02-04 22:28:15 +00:00
|
|
|
class IncludePicker;
|
|
|
|
class SourceManagerCharacterDataGetter;
|
|
|
|
|
2014-02-23 00:31:52 +00:00
|
|
|
// To set up the global state you need to parse options with OptionsParser when
|
|
|
|
// main starts and to call InitGlobals after the clang infrastructure is set up.
|
|
|
|
// The rest of this file is accessors to the data structures set up by these two
|
|
|
|
// routines.
|
|
|
|
|
|
|
|
class OptionsParser {
|
|
|
|
public:
|
|
|
|
OptionsParser(int argc, char** argv);
|
|
|
|
~OptionsParser();
|
|
|
|
|
2023-01-03 21:55:12 +00:00
|
|
|
int clang_argc() const {
|
|
|
|
return clang_argc_;
|
|
|
|
}
|
|
|
|
const char** clang_argv() const {
|
|
|
|
return clang_argv_;
|
|
|
|
}
|
2016-08-15 21:04:47 +01:00
|
|
|
|
2014-02-23 00:31:52 +00:00
|
|
|
private:
|
|
|
|
int clang_argc_;
|
|
|
|
const char** clang_argv_;
|
|
|
|
};
|
2011-04-27 00:12:44 +01:00
|
|
|
|
2016-08-15 21:24:39 +01:00
|
|
|
void InitGlobals(clang::SourceManager* sm, clang::HeaderSearch* header_search);
|
2011-02-04 22:28:15 +00:00
|
|
|
|
2011-04-27 00:12:44 +01:00
|
|
|
// Can be called by tests -- doesn't need a SourceManager or
|
|
|
|
// argc/argv. Note that GlobalSourceManager() and DefaultDataGetter()
|
|
|
|
// will assert-fail if you call this instead of InitGlobals().
|
|
|
|
void InitGlobalsAndFlagsForTesting();
|
|
|
|
|
2011-04-06 21:06:56 +01:00
|
|
|
// TODO(csilvers): put all of these in the 'globals' namespace?
|
|
|
|
|
2011-04-27 00:12:44 +01:00
|
|
|
// The verbose level indicates how chatty IWYU should be. The bigger
|
|
|
|
// the chattier.
|
|
|
|
// 0: just print the full list of header files.
|
|
|
|
// 1: like 0, and also print new and deleted header files.
|
|
|
|
// 2: like 1, and also print 'canonical' error line for each type.
|
|
|
|
// 3: like 1, and also print *every* error line.
|
|
|
|
// 4: like 3, but with sorted error lines, and print all 'reason' comments.
|
|
|
|
// 5: like 4, and add tons of debug info.
|
|
|
|
// 6: like 5, and print every symbol that's needed by main-cu files.
|
|
|
|
// 7: like 6, and print pointers of AST nodes (and uninstantiated templates)
|
2015-03-02 20:41:01 +00:00
|
|
|
// 8: like 7, and print all mappings, built-in and dynamic.
|
|
|
|
// 10: like 8, and add tons more debug info (for all non-system header files).
|
2011-04-27 00:12:44 +01:00
|
|
|
// 11: like 10, and add tons *more* debug info (for all header files).
|
|
|
|
struct CommandlineFlags {
|
2013-12-15 12:33:18 +00:00
|
|
|
enum PrefixHeaderIncludePolicy { kAdd, kKeep, kRemove };
|
2011-04-27 00:12:44 +01:00
|
|
|
CommandlineFlags(); // sets flags to default values
|
|
|
|
int ParseArgv(int argc, char** argv); // parses flags from argv
|
2022-08-28 13:00:54 +01:00
|
|
|
bool HasDebugFlag(const char* flag) const;
|
2011-04-27 00:12:44 +01:00
|
|
|
|
|
|
|
set<string> check_also; // -c: globs to report iwyu violations for
|
2019-03-07 17:45:22 +00:00
|
|
|
set<string> keep; // -k: globs to force-keep includes for
|
2011-04-27 00:13:25 +01:00
|
|
|
bool transitive_includes_only; // -t: don't add 'new' #includes to files
|
2011-04-27 00:12:44 +01:00
|
|
|
int verbose; // -v: how much information to emit as we parse
|
2012-10-14 23:39:30 +01:00
|
|
|
vector<string> mapping_files; // -m: mapping files
|
2013-03-07 15:40:22 +00:00
|
|
|
bool no_default_mappings; // -n: no default mappings
|
2015-04-25 10:20:21 +01:00
|
|
|
// Truncate output lines to this length. No short option.
|
|
|
|
int max_line_length;
|
2013-12-15 12:33:18 +00:00
|
|
|
// Policy regarding files included via -include option. No short option.
|
|
|
|
PrefixHeaderIncludePolicy prefix_header_include_policy;
|
2017-11-22 02:42:38 +00:00
|
|
|
bool pch_in_code; // Treat the first seen include as a PCH. No short option.
|
|
|
|
bool no_comments; // Disable 'why' comments. No short option.
|
2021-09-27 02:53:20 +01:00
|
|
|
bool update_comments; // Force 'why' comments. No short option.
|
2022-05-27 22:08:32 +01:00
|
|
|
bool comments_with_namespace; // Show namespace in 'why' comments.
|
2017-11-22 02:42:38 +00:00
|
|
|
bool no_fwd_decls; // Disable forward declarations.
|
2020-03-20 01:02:40 +00:00
|
|
|
bool quoted_includes_first; // Place quoted includes first in sort order.
|
2019-01-31 16:22:54 +00:00
|
|
|
bool cxx17ns; // -C: C++17 nested namespace syntax
|
2022-01-29 16:01:46 +00:00
|
|
|
int exit_code_error; // Exit with this code for iwyu violations.
|
|
|
|
int exit_code_always; // Always exit with this exit code.
|
2022-08-28 13:00:54 +01:00
|
|
|
set<string> dbg_flags; // Debug flags.
|
Add --regex option
As reported in issue #981, using std::regex in IWYU has caused a
tremendous performance regression for large mapping files containing
regex mappings.
$ cat t.cc
#include <string>
# with llvm::Regex
$ time include-what-you-use -Xiwyu --mapping_file=qt5_11.imp t.cc
...
real 0m0,529s
user 0m0,509s
sys 0m0,020s
# with std::regex
$ time include-what-you-use -Xiwyu --mapping_file=qt5_11.imp t.cc
...
real 0m29,870s
user 0m29,717s
sys 0m0,012s
qt5_11.imp contains 2300+ regex mappings, and <string> has a bunch of
includes, so this is a good testbed for regular expression engines, but
over 50x slower is not the result we were hoping for.
The reason we switched to std::regex was to get support for negative
lookaround (llvm::Regex does not have it), but exotic regexes in
mappings are pretty rare, and this is a significant performance hit.
Introduce a --regex option to select regex dialect, with documented
tradeoffs. Put the default back to LLVM's fast implementation.
This fixes issue #981.
2022-09-02 19:55:48 +01:00
|
|
|
RegexDialect regex_dialect; // Dialect for regular expression processing.
|
2011-04-27 00:12:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const CommandlineFlags& GlobalFlags();
|
|
|
|
// Used by tests as an easy way to simulate calling with different --flags.
|
|
|
|
CommandlineFlags* MutableGlobalFlagsForTesting();
|
|
|
|
|
2011-02-04 22:28:15 +00:00
|
|
|
clang::SourceManager* GlobalSourceManager();
|
|
|
|
|
|
|
|
const IncludePicker& GlobalIncludePicker();
|
|
|
|
IncludePicker* MutableGlobalIncludePicker(); // only use at great need!
|
|
|
|
|
|
|
|
const clang::PrintingPolicy& DefaultPrintPolicy();
|
|
|
|
|
|
|
|
const SourceManagerCharacterDataGetter& DefaultDataGetter();
|
|
|
|
|
2011-04-06 21:06:56 +01:00
|
|
|
// These caches record what types and decls we reported when
|
|
|
|
// instantiating a particular decl. That avoids extra work if we see
|
|
|
|
// the same decl again -- we can replay those reports, just from a new
|
|
|
|
// caller_loc.
|
|
|
|
FullUseCache* FunctionCallsFullUseCache();
|
|
|
|
FullUseCache* ClassMembersFullUseCache();
|
|
|
|
|
2011-02-04 22:28:15 +00:00
|
|
|
// These files are based on the commandline (--check_also flag plus argv).
|
|
|
|
// They are specified as glob file-patterns (which behave just as they
|
|
|
|
// do in the shell). TODO(csilvers): use a prefix instead? allow '...'?
|
|
|
|
void AddGlobToReportIWYUViolationsFor(const string& glob);
|
|
|
|
bool ShouldReportIWYUViolationsFor(const clang::FileEntry* file);
|
|
|
|
|
2019-03-07 17:45:22 +00:00
|
|
|
// For the commandline option --keep.
|
|
|
|
// Similar to AddGlobToReportIWYUViolationsFor.
|
|
|
|
void AddGlobToKeepIncludes(const string& glob);
|
|
|
|
bool ShouldKeepIncludeFor(const clang::FileEntry* file);
|
|
|
|
|
2011-02-04 22:28:15 +00:00
|
|
|
} // namespace include_what_you_use
|
|
|
|
|
2016-05-22 09:06:36 +01:00
|
|
|
#endif // INCLUDE_WHAT_YOU_USE_IWYU_GLOBALS_H_
|