2011-02-04 22:28:15 +00:00
|
|
|
//===--- iwyu_string_util.cpp - global variables for include-what-you-use -===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-08 06:12:32 +00:00
|
|
|
#include "port.h"
|
2011-02-04 22:28:15 +00:00
|
|
|
#include "iwyu_globals.h"
|
2011-02-08 06:12:32 +00:00
|
|
|
#ifndef _MSC_VER // _MSC_VER gets its own fnmatch from ./port.h
|
2011-02-04 22:28:15 +00:00
|
|
|
#include <fnmatch.h>
|
2011-02-08 06:12:32 +00:00
|
|
|
#endif
|
2011-04-27 00:12:44 +01:00
|
|
|
#include <getopt.h>
|
2011-04-27 00:03:43 +01:00
|
|
|
#include <algorithm>
|
2011-02-04 22:28:15 +00:00
|
|
|
#include <set>
|
|
|
|
#include <string>
|
2011-03-18 07:00:52 +00:00
|
|
|
#include "clang/Lex/HeaderSearch.h"
|
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"
|
|
|
|
#include "iwyu_lexer_utils.h"
|
|
|
|
#include "iwyu_location_util.h"
|
2011-03-26 22:19:34 +00:00
|
|
|
#include "iwyu_output.h"
|
2011-02-04 22:28:15 +00:00
|
|
|
#include "iwyu_stl_util.h"
|
|
|
|
|
2011-03-18 07:00:52 +00:00
|
|
|
using clang::DirectoryEntry;
|
|
|
|
using clang::DirectoryLookup;
|
2011-02-04 22:28:15 +00:00
|
|
|
using std::set;
|
|
|
|
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;
|
2011-03-18 07:00:52 +00:00
|
|
|
static vector<string>* search_paths = NULL;
|
2011-02-04 22:28:15 +00:00
|
|
|
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;
|
2011-04-27 00:12:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
static void PrintHelp(const char* extra_msg) {
|
|
|
|
printf("USAGE: iwyu [-Xiwyu --iwyu_opt]... <clang opts> <source file>\n"
|
|
|
|
"Here are the <opts> you can specify:\n"
|
|
|
|
" --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"
|
|
|
|
" --help: prints this help and exits.\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"
|
|
|
|
" --verbose=<level>: the higher the level, the more output.\n");
|
|
|
|
if (extra_msg)
|
|
|
|
printf("\n%s\n\n", extra_msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
CommandlineFlags::CommandlineFlags()
|
|
|
|
: check_also(),
|
|
|
|
howtodebug(CommandlineFlags::kUnspecified),
|
|
|
|
cwd(""),
|
|
|
|
verbose(getenv("IWYU_VERBOSE") ? atoi(getenv("IWYU_VERBOSE")) : 1) {
|
|
|
|
}
|
|
|
|
|
|
|
|
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'},
|
|
|
|
{"help", no_argument, NULL, 'h'},
|
|
|
|
{"cwd", required_argument, NULL, 'p'},
|
|
|
|
{"verbose", required_argument, NULL, 'v'},
|
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
static const char shortopts[] = "d::p:v:c:";
|
|
|
|
int option_index;
|
|
|
|
while (true) {
|
|
|
|
switch (getopt_long(argc, argv, shortopts, longopts, &option_index)) {
|
|
|
|
case 'c': AddGlobToReportIWYUViolationsFor(optarg); break;
|
|
|
|
case 'd': howtodebug = optarg ? optarg : ""; break;
|
|
|
|
case 'h': PrintHelp(""); exit(0); break;
|
|
|
|
case 'p': cwd = optarg; break;
|
|
|
|
case 'v': verbose = atoi(optarg); break;
|
|
|
|
case -1: return optind; // means 'no more input'
|
|
|
|
default: PrintHelp("FATAL ERROR: unknown flag."); exit(1); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return optind; // unreachable
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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>";
|
|
|
|
|
|
|
|
// Handles all iwyu-specific flags, like --verbose.
|
|
|
|
int ParseIwyuCommandlineFlags(int argc, char** argv) {
|
|
|
|
CHECK_(commandline_flags == NULL && "Only parse commandline flags once");
|
|
|
|
commandline_flags = new CommandlineFlags;
|
|
|
|
const int retval = commandline_flags->ParseArgv(argc, argv);
|
|
|
|
|
|
|
|
if (!commandline_flags.cwd.empty()) {
|
|
|
|
printf("-p/--cwd not yet implemented\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (commandline_flags.howtodebug != CommandlineFlags::kUnspecified) {
|
|
|
|
printf("-d/--howtodebug not yet implemented\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
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>.
|
|
|
|
static bool SortByDescendingLength(const string& left, const string& right) {
|
|
|
|
return left.length() > right.length();
|
|
|
|
}
|
|
|
|
|
2011-04-27 00:03:43 +01:00
|
|
|
// Sorts them by descending length, does other kinds of cleanup.
|
|
|
|
static void NormalizeSystemIncludeDirectories(vector<string>* include_dirs) {
|
|
|
|
sort(include_dirs->begin(), include_dirs->end(), &SortByDescendingLength);
|
|
|
|
}
|
|
|
|
|
2011-03-18 07:01:07 +00:00
|
|
|
static vector<string>* ComputeSystemIncludeDirectories(
|
|
|
|
clang::HeaderSearch* header_search) {
|
2011-03-18 07:00:52 +00:00
|
|
|
vector<string>* system_include_dirs = new vector<string>;
|
|
|
|
for (clang::HeaderSearch::search_dir_iterator
|
|
|
|
it = header_search->search_dir_begin();
|
|
|
|
it != header_search->search_dir_end(); ++it) {
|
|
|
|
const DirectoryEntry * entry = it->getDir();
|
|
|
|
if (entry != NULL) {
|
|
|
|
system_include_dirs->push_back(entry->getName());
|
|
|
|
}
|
|
|
|
}
|
2011-04-27 00:03:43 +01:00
|
|
|
NormalizeSystemIncludeDirectories(system_include_dirs);
|
2011-03-18 07:00:52 +00:00
|
|
|
return system_include_dirs;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitGlobals(clang::SourceManager* sm, 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-03-18 07:01:07 +00:00
|
|
|
search_paths = ComputeSystemIncludeDirectories(header_search);
|
2011-02-04 22:28:15 +00:00
|
|
|
include_picker = new IncludePicker;
|
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
|
|
|
|
|
|
|
for (Each<string> it(search_paths); !it.AtEnd(); ++it)
|
|
|
|
VERRS(6) << "Search path: " << *it << "\n";
|
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;
|
|
|
|
}
|
|
|
|
|
2011-03-18 07:00:52 +00:00
|
|
|
const vector<string>& GlobalSearchPaths() {
|
|
|
|
assert(search_paths && "Must call InitGlobals() before calling this");
|
|
|
|
return *search_paths;
|
|
|
|
}
|
|
|
|
|
2011-02-04 22:28:15 +00:00
|
|
|
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");
|
|
|
|
commandline_flags->check_also.insert(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-02-04 22:28:15 +00:00
|
|
|
if (fnmatch(it->c_str(), filepath.c_str(), FNM_PATHNAME) == 0)
|
|
|
|
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;
|
|
|
|
include_picker = new IncludePicker;
|
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.
|
|
|
|
search_paths = new vector<string>;
|
2011-03-18 07:01:07 +00:00
|
|
|
search_paths->push_back("/usr/include");
|
2011-03-18 07:00:52 +00:00
|
|
|
search_paths->push_back("/usr/include/c++/4.3");
|
|
|
|
search_paths->push_back("/usr/include/c++/4.2");
|
|
|
|
search_paths->push_back(".");
|
|
|
|
search_paths->push_back("/usr/src/linux-headers-2.6.24-gg23/include");
|
2011-03-18 07:01:07 +00:00
|
|
|
|
2011-04-27 00:03:43 +01:00
|
|
|
NormalizeSystemIncludeDirectories(search_paths);
|
2011-03-18 07:00:41 +00:00
|
|
|
}
|
|
|
|
|
2011-02-04 22:28:15 +00:00
|
|
|
} // namespace include_what_you_use
|