2012-11-25 22:09:37 +00:00
|
|
|
//===--- iwyu_globals.cc - 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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "iwyu_globals.h"
|
2011-05-04 19:29:59 +01:00
|
|
|
|
2011-05-24 00:07:01 +01:00
|
|
|
#include <stdio.h> // for printf
|
|
|
|
#include <stdlib.h> // for atoi, exit, getenv
|
|
|
|
#include <algorithm> // for sort, make_pair
|
|
|
|
#include <map> // for map
|
|
|
|
#include <set> // for set
|
|
|
|
#include <string> // for string, operator<, etc
|
|
|
|
#include <utility> // for make_pair, pair
|
2011-05-04 19:29:59 +01:00
|
|
|
|
2011-04-06 21:06:56 +01:00
|
|
|
#include "iwyu_cache.h"
|
2011-02-04 22:28:15 +00:00
|
|
|
#include "iwyu_include_picker.h"
|
2011-05-26 00:01:44 +01:00
|
|
|
#include "iwyu_getopt.h"
|
2011-02-04 22:28:15 +00:00
|
|
|
#include "iwyu_lexer_utils.h"
|
|
|
|
#include "iwyu_location_util.h"
|
2011-12-01 02:30:18 +00:00
|
|
|
#include "iwyu_path_util.h"
|
2011-02-04 22:28:15 +00:00
|
|
|
#include "iwyu_stl_util.h"
|
2011-05-04 19:29:59 +01:00
|
|
|
#include "iwyu_string_util.h"
|
2011-12-01 02:30:18 +00:00
|
|
|
#include "iwyu_verrs.h"
|
2014-02-23 00:31:52 +00:00
|
|
|
#include "iwyu_version.h"
|
2011-05-24 00:07:01 +01:00
|
|
|
#include "port.h" // for CHECK_, etc
|
2011-05-04 19:29:59 +01:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "clang/AST/PrettyPrinter.h"
|
|
|
|
#include "clang/Basic/FileManager.h"
|
2014-02-23 00:31:52 +00:00
|
|
|
#include "clang/Basic/Version.h"
|
2011-05-04 19:29:59 +01:00
|
|
|
#include "clang/Lex/HeaderSearch.h"
|
2011-02-04 22:28:15 +00:00
|
|
|
|
2011-03-18 07:00:52 +00:00
|
|
|
using clang::DirectoryEntry;
|
2011-04-27 00:59:45 +01:00
|
|
|
using std::make_pair;
|
2011-04-27 00:21:51 +01:00
|
|
|
using std::map;
|
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
|
|
|
|
|
|
|
namespace include_what_you_use {
|
|
|
|
|
2011-04-27 00:12:44 +01:00
|
|
|
static CommandlineFlags* commandline_flags = NULL;
|
2011-02-04 22:28:15 +00:00
|
|
|
static clang::SourceManager* source_manager = NULL;
|
|
|
|
static IncludePicker* include_picker = NULL;
|
|
|
|
static const clang::LangOptions default_lang_options;
|
|
|
|
static const clang::PrintingPolicy default_print_policy(default_lang_options);
|
|
|
|
static SourceManagerCharacterDataGetter* data_getter = NULL;
|
2011-04-06 21:06:56 +01:00
|
|
|
static FullUseCache* function_calls_full_use_cache = NULL;
|
|
|
|
static FullUseCache* class_members_full_use_cache = NULL;
|
2014-02-23 00:31:52 +00:00
|
|
|
static int ParseIwyuCommandlineFlags(int argc, char** argv);
|
|
|
|
static int ParseInterceptedCommandlineFlags(int argc, char** argv);
|
2011-04-27 00:12:44 +01:00
|
|
|
|
|
|
|
static void PrintHelp(const char* extra_msg) {
|
2012-12-23 13:44:51 +00:00
|
|
|
printf("USAGE: include-what-you-use [-Xiwyu --iwyu_opt]... <clang opts>"
|
|
|
|
" <source file>\n"
|
2013-01-07 21:38:14 +00:00
|
|
|
"Here are the <iwyu_opts> you can specify (e.g. -Xiwyu --verbose=3):\n"
|
2011-04-27 00:12:44 +01:00
|
|
|
" --check_also=<glob>: tells iwyu to print iwyu-violation info\n"
|
|
|
|
" for all files matching the given glob pattern (in addition\n"
|
|
|
|
" to the default of reporting for the input .cc file and its\n"
|
|
|
|
" associated .h files). This flag may be specified multiple\n"
|
|
|
|
" times to specify multiple glob patterns.\n"
|
|
|
|
" --cwd=<dir>: tells iwyu what the current working directory is.\n"
|
|
|
|
" --howtodebug[=<filename>]: with no arg, prints instructions on\n"
|
|
|
|
" how to run iwyu under gdb for the input file, and exits.\n"
|
|
|
|
" With an arg, prints only when input file matches the arg.\n"
|
2012-10-14 23:39:30 +01:00
|
|
|
" --mapping_file=<filename>: gives iwyu a mapping file.\n"
|
2013-03-07 15:40:22 +00:00
|
|
|
" --no_default_mappings: do not add iwyu's default mappings.\n"
|
2011-04-27 00:13:25 +01:00
|
|
|
" --transitive_includes_only: do not suggest that a file add\n"
|
|
|
|
" foo.h unless foo.h is already visible in the file's\n"
|
|
|
|
" transitive includes.\n"
|
2013-12-15 12:33:18 +00:00
|
|
|
" --prefix_header_includes=<value>: tells iwyu what to do with\n"
|
|
|
|
" in-source includes and forward declarations involving\n"
|
|
|
|
" prefix headers. Prefix header is a file included via\n"
|
|
|
|
" command-line option -include. If prefix header makes\n"
|
|
|
|
" include or forward declaration obsolete, presence of such\n"
|
|
|
|
" include can be controlled with the following values\n"
|
|
|
|
" add: new lines are added\n"
|
|
|
|
" keep: new lines aren't added, existing are kept intact\n"
|
|
|
|
" remove: new lines aren't added, existing are removed\n"
|
|
|
|
" Default value is 'add'.\n"
|
2014-04-27 21:35:44 +01:00
|
|
|
" --pch_in_code: mark the first include in a translation unit as a\n"
|
|
|
|
" precompiled header. Use --pch_in_code to prevent IWYU from\n"
|
|
|
|
" removing necessary PCH includes. Though Clang forces PCHs\n"
|
|
|
|
" to be listed as prefix headers, the PCH-in-code pattern can\n"
|
|
|
|
" be used with GCC and is standard practice on MSVC\n"
|
|
|
|
" (e.g. stdafx.h).\n"
|
2014-02-23 00:31:52 +00:00
|
|
|
" --verbose=<level>: the higher the level, the more output.\n"
|
|
|
|
"\n"
|
|
|
|
"In addition to IWYU-specific options you can specify the following\n"
|
|
|
|
"options without -Xiwyu prefix:\n"
|
|
|
|
" --help: prints this help and exits.\n"
|
|
|
|
" --version: prints version and exits.\n");
|
2011-04-27 00:12:44 +01:00
|
|
|
if (extra_msg)
|
|
|
|
printf("\n%s\n\n", extra_msg);
|
|
|
|
}
|
|
|
|
|
2014-02-23 00:31:52 +00:00
|
|
|
static void PrintVersion() {
|
|
|
|
llvm::outs() << "include-what-you-use " << IWYU_VERSION_STRING;
|
|
|
|
// IWYU_SVN_REVISION should be provided by build system.
|
|
|
|
string iwyu_svn_revision = IWYU_SVN_REVISION;
|
|
|
|
if (!iwyu_svn_revision.empty()) {
|
|
|
|
llvm::outs() << " (" << iwyu_svn_revision << ")";
|
|
|
|
}
|
|
|
|
llvm::outs() << " based on " << clang::getClangFullVersion()
|
|
|
|
<< "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
OptionsParser::OptionsParser(int argc, char** argv) {
|
|
|
|
// Separate out iwyu-specific, intercepted, and clang flags. iwyu-specific
|
|
|
|
// flags are "-Xiwyu <iwyu_flag>", intercepted flags are usual clang flags
|
|
|
|
// like --version, --help, which we intercept to provide custom handling.
|
|
|
|
char** iwyu_argv = new char*[argc + 1];
|
|
|
|
iwyu_argv[0] = argv[0];
|
|
|
|
int iwyu_argc = 1;
|
|
|
|
char** intercepted_argv = new char*[argc + 1];
|
|
|
|
intercepted_argv[0] = argv[0];
|
|
|
|
int intercepted_argc = 1;
|
|
|
|
clang_argv_ = new const char*[argc + 1];
|
|
|
|
clang_argv_[0] = argv[0];
|
|
|
|
clang_argc_ = 1;
|
|
|
|
for (int i = 1; i < argc; ++i) {
|
|
|
|
if (i < argc - 1 && strcmp(argv[i], "-Xiwyu") == 0)
|
|
|
|
iwyu_argv[iwyu_argc++] = argv[++i]; // the word after -Xiwyu
|
|
|
|
else if (strcmp(argv[i], "--help") == 0)
|
|
|
|
intercepted_argv[intercepted_argc++] = argv[i]; // intercept --help
|
|
|
|
else if (strcmp(argv[i], "--version") == 0)
|
|
|
|
intercepted_argv[intercepted_argc++] = argv[i]; // intercept --version
|
|
|
|
else
|
|
|
|
clang_argv_[clang_argc_++] = argv[i];
|
|
|
|
}
|
|
|
|
// argv should be NULL-terminated
|
|
|
|
iwyu_argv[iwyu_argc] = NULL;
|
|
|
|
intercepted_argv[intercepted_argc] = NULL;
|
|
|
|
clang_argv_[clang_argc_] = NULL;
|
|
|
|
|
|
|
|
ParseInterceptedCommandlineFlags(intercepted_argc, intercepted_argv);
|
|
|
|
ParseIwyuCommandlineFlags(iwyu_argc, iwyu_argv);
|
|
|
|
|
|
|
|
delete [] iwyu_argv;
|
|
|
|
delete [] intercepted_argv;
|
|
|
|
}
|
|
|
|
|
|
|
|
OptionsParser::~OptionsParser() {
|
|
|
|
delete [] clang_argv_;
|
|
|
|
}
|
|
|
|
|
2011-04-27 00:12:44 +01:00
|
|
|
CommandlineFlags::CommandlineFlags()
|
|
|
|
: check_also(),
|
|
|
|
howtodebug(CommandlineFlags::kUnspecified),
|
|
|
|
cwd(""),
|
2011-04-27 00:13:25 +01:00
|
|
|
transitive_includes_only(false),
|
2013-03-07 15:40:22 +00:00
|
|
|
verbose(getenv("IWYU_VERBOSE") ? atoi(getenv("IWYU_VERBOSE")) : 1),
|
2013-12-15 12:33:18 +00:00
|
|
|
no_default_mappings(false),
|
2014-04-27 21:35:44 +01:00
|
|
|
prefix_header_include_policy(CommandlineFlags::kAdd),
|
|
|
|
pch_in_code(false) {
|
2011-04-27 00:12:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int CommandlineFlags::ParseArgv(int argc, char** argv) {
|
|
|
|
static const struct option longopts[] = {
|
|
|
|
{"check_also", required_argument, NULL, 'c'}, // can be specified >once
|
|
|
|
{"howtodebug", optional_argument, NULL, 'd'},
|
|
|
|
{"cwd", required_argument, NULL, 'p'},
|
2011-04-27 00:13:25 +01:00
|
|
|
{"transitive_includes_only", no_argument, NULL, 't'},
|
2011-04-27 00:12:44 +01:00
|
|
|
{"verbose", required_argument, NULL, 'v'},
|
2012-10-14 23:39:30 +01:00
|
|
|
{"mapping_file", required_argument, NULL, 'm'},
|
2013-03-07 15:40:22 +00:00
|
|
|
{"no_default_mappings", no_argument, NULL, 'n'},
|
2013-12-15 12:33:18 +00:00
|
|
|
{"prefix_header_includes", required_argument, NULL, 'x'},
|
2014-04-27 21:35:44 +01:00
|
|
|
{"pch_in_code", no_argument, NULL, 'h'},
|
2011-04-27 00:12:44 +01:00
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
2013-03-07 15:40:22 +00:00
|
|
|
static const char shortopts[] = "d::p:v:c:m:n";
|
2011-04-27 00:12:44 +01:00
|
|
|
while (true) {
|
2012-08-11 19:15:00 +01:00
|
|
|
switch (getopt_long(argc, argv, shortopts, longopts, NULL)) {
|
2011-04-27 00:12:44 +01:00
|
|
|
case 'c': AddGlobToReportIWYUViolationsFor(optarg); break;
|
|
|
|
case 'd': howtodebug = optarg ? optarg : ""; break;
|
|
|
|
case 'p': cwd = optarg; break;
|
2011-04-27 00:13:25 +01:00
|
|
|
case 't': transitive_includes_only = true; break;
|
2011-04-27 00:12:44 +01:00
|
|
|
case 'v': verbose = atoi(optarg); break;
|
2012-10-14 23:39:30 +01:00
|
|
|
case 'm': mapping_files.push_back(optarg); break;
|
2013-03-07 15:40:22 +00:00
|
|
|
case 'n': no_default_mappings = true; break;
|
2013-12-15 12:33:18 +00:00
|
|
|
case 'x':
|
|
|
|
if (strcmp(optarg, "add") == 0) {
|
|
|
|
prefix_header_include_policy = CommandlineFlags::kAdd;
|
|
|
|
} else if (strcmp(optarg, "keep") == 0) {
|
|
|
|
prefix_header_include_policy = CommandlineFlags::kKeep;
|
|
|
|
} else if (strcmp(optarg, "remove") == 0) {
|
|
|
|
prefix_header_include_policy = CommandlineFlags::kRemove;
|
|
|
|
} else {
|
|
|
|
PrintHelp("FATAL ERROR: unknown --prefix_header_includes value.");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
2014-04-27 21:35:44 +01:00
|
|
|
case 'h': pch_in_code = true; break;
|
2011-04-27 00:12:44 +01:00
|
|
|
case -1: return optind; // means 'no more input'
|
|
|
|
default: PrintHelp("FATAL ERROR: unknown flag."); exit(1); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return optind; // unreachable
|
|
|
|
}
|
|
|
|
|
2014-02-23 00:31:52 +00:00
|
|
|
// Though option -v prints version too, it isn't intercepted because it also
|
|
|
|
// provides other functionality like printing clang invocation, header search
|
|
|
|
// paths.
|
|
|
|
// TODO(vsapsai): provide IWYU version in Driver::PrintVersion when version
|
|
|
|
// callbacks are supported (see FIXME in Driver::PrintVersion).
|
|
|
|
static int ParseInterceptedCommandlineFlags(int argc, char** argv) {
|
|
|
|
static const struct option longopts[] = {
|
|
|
|
{"help", no_argument, NULL, 'h'},
|
|
|
|
{"version", no_argument, NULL, 'v'},
|
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
static const char shortopts[] = "";
|
|
|
|
while (true) {
|
|
|
|
switch (getopt_long(argc, argv, shortopts, longopts, NULL)) {
|
|
|
|
case 'h': PrintHelp(""); exit(0); break;
|
|
|
|
case 'v': PrintVersion(); exit(0); break;
|
|
|
|
case -1: return optind; // means 'no more input'
|
|
|
|
default: PrintHelp("FATAL ERROR: unknown flag."); exit(1); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return optind; // unreachable
|
|
|
|
}
|
|
|
|
|
2011-04-27 00:12:44 +01:00
|
|
|
// The default value for the --howtodebug flag. Indicates that the
|
|
|
|
// flag isn't present. It's a special, reserved value, and a user
|
|
|
|
// isn't expected to type it directly.
|
|
|
|
const char CommandlineFlags::kUnspecified[] = "<flag-unspecified>";
|
|
|
|
|
2014-02-23 00:31:52 +00:00
|
|
|
// Handles all iwyu-specific flags, like --verbose. Returns the index into
|
|
|
|
// argv past all the iwyu commandline flags.
|
|
|
|
static int ParseIwyuCommandlineFlags(int argc, char** argv) {
|
2011-04-27 00:12:44 +01:00
|
|
|
CHECK_(commandline_flags == NULL && "Only parse commandline flags once");
|
|
|
|
commandline_flags = new CommandlineFlags;
|
|
|
|
const int retval = commandline_flags->ParseArgv(argc, argv);
|
2011-12-01 02:30:18 +00:00
|
|
|
SetVerboseLevel(commandline_flags->verbose);
|
2011-04-27 00:12:44 +01:00
|
|
|
|
2014-02-23 00:41:21 +00:00
|
|
|
if (!commandline_flags->cwd.empty()) {
|
|
|
|
printf("-p/--cwd not yet implemented\n");
|
|
|
|
exit(1);
|
2011-04-27 00:12:44 +01:00
|
|
|
}
|
2011-04-27 00:59:45 +01:00
|
|
|
if (commandline_flags->howtodebug != CommandlineFlags::kUnspecified) {
|
2014-02-23 00:41:21 +00:00
|
|
|
printf("-d/--howtodebug not yet implemented\n");
|
|
|
|
exit(1);
|
2011-04-27 00:12:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
VERRS(4) << "Setting verbose-level to " << commandline_flags->verbose << "\n";
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
2011-02-04 22:28:15 +00:00
|
|
|
|
2011-03-18 07:01:07 +00:00
|
|
|
// Make sure we put longer search-paths first, so iwyu will map
|
|
|
|
// /usr/include/c++/4.4/foo to <foo> rather than <c++/4.4/foo>.
|
2011-04-27 00:21:51 +01:00
|
|
|
static bool SortByDescendingLength(const HeaderSearchPath& left,
|
|
|
|
const HeaderSearchPath& right) {
|
|
|
|
return left.path.length() > right.path.length();
|
2011-03-18 07:01:07 +00:00
|
|
|
}
|
|
|
|
|
2011-04-27 00:03:43 +01:00
|
|
|
// Sorts them by descending length, does other kinds of cleanup.
|
2011-04-27 00:21:51 +01:00
|
|
|
static vector<HeaderSearchPath> NormalizeHeaderSearchPaths(
|
|
|
|
const map<string, HeaderSearchPath::Type>& include_dirs_map) {
|
|
|
|
vector<HeaderSearchPath> include_dirs;
|
|
|
|
for (Each<string, HeaderSearchPath::Type>
|
|
|
|
it(&include_dirs_map); !it.AtEnd(); ++it) {
|
|
|
|
include_dirs.push_back(HeaderSearchPath(it->first, it->second));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sort(include_dirs.begin(), include_dirs.end(), &SortByDescendingLength);
|
|
|
|
return include_dirs;
|
2011-04-27 00:03:43 +01:00
|
|
|
}
|
|
|
|
|
2011-04-27 00:21:51 +01:00
|
|
|
// Asks clang what the search-paths are for include files, normalizes
|
|
|
|
// them, and returns them in a vector.
|
|
|
|
static vector<HeaderSearchPath> ComputeHeaderSearchPaths(
|
2011-03-18 07:01:07 +00:00
|
|
|
clang::HeaderSearch* header_search) {
|
2011-04-27 00:21:51 +01:00
|
|
|
map<string, HeaderSearchPath::Type> search_path_map;
|
|
|
|
for (clang::HeaderSearch::search_dir_iterator
|
|
|
|
it = header_search->system_dir_begin();
|
|
|
|
it != header_search->system_dir_end(); ++it) {
|
2011-08-01 21:58:24 +01:00
|
|
|
if (const DirectoryEntry* entry = it->getDir()) {
|
|
|
|
const string path = CanonicalizeFilePath(entry->getName());
|
|
|
|
search_path_map[path] = HeaderSearchPath::kSystemPath;
|
|
|
|
}
|
2011-04-27 00:21:51 +01:00
|
|
|
}
|
2011-03-18 07:00:52 +00:00
|
|
|
for (clang::HeaderSearch::search_dir_iterator
|
|
|
|
it = header_search->search_dir_begin();
|
|
|
|
it != header_search->search_dir_end(); ++it) {
|
2011-04-27 00:21:51 +01:00
|
|
|
if (const DirectoryEntry* entry = it->getDir()) {
|
|
|
|
// search_dir_begin()/end() includes both system and user paths.
|
|
|
|
// If it's a system path, it's already in the map, so everything
|
|
|
|
// new is a user path. The insert only 'takes' for new entries.
|
2011-08-01 21:58:24 +01:00
|
|
|
const string path = CanonicalizeFilePath(entry->getName());
|
|
|
|
search_path_map.insert(make_pair(path, HeaderSearchPath::kUserPath));
|
2011-03-18 07:00:52 +00:00
|
|
|
}
|
|
|
|
}
|
2011-04-27 00:21:51 +01:00
|
|
|
return NormalizeHeaderSearchPaths(search_path_map);
|
2011-03-18 07:00:52 +00:00
|
|
|
}
|
|
|
|
|
2012-11-25 21:26:06 +00:00
|
|
|
void InitGlobals(clang::SourceManager* sm,
|
2013-02-26 19:12:31 +00:00
|
|
|
clang::HeaderSearch* header_search) {
|
2011-03-26 22:19:34 +00:00
|
|
|
CHECK_(sm && "InitGlobals() needs a non-NULL SourceManager");
|
2011-02-04 22:28:15 +00:00
|
|
|
source_manager = sm;
|
|
|
|
data_getter = new SourceManagerCharacterDataGetter(*source_manager);
|
2011-12-01 02:30:18 +00:00
|
|
|
vector<HeaderSearchPath> search_paths =
|
|
|
|
ComputeHeaderSearchPaths(header_search);
|
|
|
|
SetHeaderSearchPaths(search_paths);
|
2013-03-07 15:40:22 +00:00
|
|
|
include_picker = new IncludePicker(GlobalFlags().no_default_mappings);
|
2011-04-06 21:06:56 +01:00
|
|
|
function_calls_full_use_cache = new FullUseCache;
|
|
|
|
class_members_full_use_cache = new FullUseCache;
|
2011-03-26 22:19:34 +00:00
|
|
|
|
2011-12-01 02:30:18 +00:00
|
|
|
for (Each<HeaderSearchPath> it(&search_paths); !it.AtEnd(); ++it) {
|
2011-04-27 00:21:51 +01:00
|
|
|
const char* path_type_name
|
|
|
|
= (it->path_type == HeaderSearchPath::kSystemPath ? "system" : "user");
|
|
|
|
VERRS(6) << "Search path: " << it->path << " (" << path_type_name << ")\n";
|
|
|
|
}
|
2012-10-14 23:39:30 +01:00
|
|
|
|
2012-11-25 21:26:06 +00:00
|
|
|
// Add mappings.
|
2012-10-14 23:39:30 +01:00
|
|
|
for (Each<string> it(&GlobalFlags().mapping_files); !it.AtEnd(); ++it) {
|
|
|
|
include_picker->AddMappingsFromFile(*it);
|
|
|
|
}
|
2011-02-04 22:28:15 +00:00
|
|
|
}
|
|
|
|
|
2011-04-27 00:12:44 +01:00
|
|
|
const CommandlineFlags& GlobalFlags() {
|
|
|
|
CHECK_(commandline_flags && "Call ParseIwyuCommandlineFlags() before this");
|
|
|
|
return *commandline_flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
CommandlineFlags* MutableGlobalFlagsForTesting() {
|
|
|
|
CHECK_(commandline_flags && "Call ParseIwyuCommandlineFlags() before this");
|
|
|
|
return commandline_flags;
|
|
|
|
}
|
|
|
|
|
2011-02-04 22:28:15 +00:00
|
|
|
clang::SourceManager* GlobalSourceManager() {
|
2011-03-04 00:29:56 +00:00
|
|
|
CHECK_(source_manager && "Must call InitGlobals() before calling this");
|
2011-02-04 22:28:15 +00:00
|
|
|
return source_manager;
|
|
|
|
}
|
|
|
|
|
|
|
|
const IncludePicker& GlobalIncludePicker() {
|
2011-03-04 00:29:56 +00:00
|
|
|
CHECK_(include_picker && "Must call InitGlobals() before calling this");
|
2011-02-04 22:28:15 +00:00
|
|
|
return *include_picker;
|
|
|
|
}
|
|
|
|
|
|
|
|
IncludePicker* MutableGlobalIncludePicker() {
|
2011-03-04 00:29:56 +00:00
|
|
|
CHECK_(include_picker && "Must call InitGlobals() before calling this");
|
2011-02-04 22:28:15 +00:00
|
|
|
return include_picker;
|
|
|
|
}
|
|
|
|
|
|
|
|
const clang::PrintingPolicy& DefaultPrintPolicy() {
|
|
|
|
return default_print_policy;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SourceManagerCharacterDataGetter& DefaultDataGetter() {
|
2011-03-04 00:29:56 +00:00
|
|
|
CHECK_(data_getter && "Must call InitGlobals() before calling this");
|
2011-02-04 22:28:15 +00:00
|
|
|
return *data_getter;
|
|
|
|
}
|
|
|
|
|
2011-04-06 21:06:56 +01:00
|
|
|
FullUseCache* FunctionCallsFullUseCache() {
|
|
|
|
return function_calls_full_use_cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
FullUseCache* ClassMembersFullUseCache() {
|
|
|
|
return class_members_full_use_cache;
|
|
|
|
}
|
|
|
|
|
2011-02-04 22:28:15 +00:00
|
|
|
void AddGlobToReportIWYUViolationsFor(const string& glob) {
|
2011-04-27 00:12:44 +01:00
|
|
|
CHECK_(commandline_flags && "Call ParseIwyuCommandlineFlags() before this");
|
2011-07-20 23:53:00 +01:00
|
|
|
commandline_flags->check_also.insert(CanonicalizeFilePath(glob));
|
2011-02-04 22:28:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ShouldReportIWYUViolationsFor(const clang::FileEntry* file) {
|
|
|
|
const string filepath = GetFilePath(file);
|
2011-04-27 00:12:44 +01:00
|
|
|
for (Each<string> it(&GlobalFlags().check_also); !it.AtEnd(); ++it)
|
2011-05-25 00:12:28 +01:00
|
|
|
if (GlobMatchesPath(it->c_str(), filepath.c_str()))
|
2011-02-04 22:28:15 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-04-27 00:12:44 +01:00
|
|
|
void InitGlobalsAndFlagsForTesting() {
|
|
|
|
CHECK_(commandline_flags == NULL && "Only parse commandline flags once");
|
|
|
|
CHECK_(include_picker == NULL && "Only call InitGlobals[ForTesting] once");
|
|
|
|
commandline_flags = new CommandlineFlags;
|
2011-03-18 07:00:41 +00:00
|
|
|
source_manager = NULL;
|
|
|
|
data_getter = NULL;
|
2013-03-07 15:40:22 +00:00
|
|
|
include_picker = new IncludePicker(GlobalFlags().no_default_mappings);
|
2011-04-06 21:06:56 +01:00
|
|
|
function_calls_full_use_cache = new FullUseCache;
|
|
|
|
class_members_full_use_cache = new FullUseCache;
|
2011-03-18 07:01:07 +00:00
|
|
|
|
2011-03-18 07:00:52 +00:00
|
|
|
// Use a reasonable default for the -I flags.
|
2011-04-27 00:21:51 +01:00
|
|
|
map<string, HeaderSearchPath::Type> search_path_map;
|
|
|
|
search_path_map["/usr/include"] = HeaderSearchPath::kSystemPath;
|
|
|
|
search_path_map["/usr/include/c++/4.3"] = HeaderSearchPath::kSystemPath;
|
|
|
|
search_path_map["/usr/include/c++/4.2"] = HeaderSearchPath::kSystemPath;
|
|
|
|
search_path_map["."] = HeaderSearchPath::kUserPath;
|
|
|
|
search_path_map["/usr/src/linux-headers-2.6.24-gg23/include"] = HeaderSearchPath::kSystemPath;
|
|
|
|
|
2011-12-01 02:30:18 +00:00
|
|
|
SetHeaderSearchPaths(NormalizeHeaderSearchPaths(search_path_map));
|
2011-03-18 07:00:41 +00:00
|
|
|
}
|
|
|
|
|
2011-02-04 22:28:15 +00:00
|
|
|
} // namespace include_what_you_use
|