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: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 {
|
|
|
|
|
|
|
|
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-02-04 22:28:15 +00:00
|
|
|
static set<string>* globs_to_report_iwyu_violations_for = NULL;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
if (globs_to_report_iwyu_violations_for == NULL)
|
|
|
|
globs_to_report_iwyu_violations_for = new set<string>;
|
|
|
|
globs_to_report_iwyu_violations_for->insert(glob);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShouldReportIWYUViolationsFor(const clang::FileEntry* file) {
|
|
|
|
if (globs_to_report_iwyu_violations_for == NULL)
|
|
|
|
return false;
|
|
|
|
const string filepath = GetFilePath(file);
|
|
|
|
for (Each<string> it(globs_to_report_iwyu_violations_for); !it.AtEnd(); ++it)
|
|
|
|
if (fnmatch(it->c_str(), filepath.c_str(), FNM_PATHNAME) == 0)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-03-18 07:00:41 +00:00
|
|
|
void InitGlobalsForTesting() {
|
|
|
|
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
|