diff options
author | Kim Gräsman <kim.grasman@gmail.com> | 2023-04-16 18:33:43 +0200 |
---|---|---|
committer | Kim Gräsman <kim.grasman@gmail.com> | 2023-04-16 20:08:59 +0200 |
commit | 1646cfbfbdf35e6f59f8c8c76fd3ebf38fecdde1 (patch) | |
tree | fc6ba24dcdca84e9c798adede931294905033d83 | |
parent | c6976de83a0c57cfd45ab4cf62d7bc70752122c5 (diff) |
Add functions to derive standard libs from compilation
Pass CompilerInstance directly to InitGlobals to give it access to the
entire compilation state.
No functional change.
-rw-r--r-- | iwyu.cc | 3 | ||||
-rw-r--r-- | iwyu_globals.cc | 36 | ||||
-rw-r--r-- | iwyu_globals.h | 2 |
3 files changed, 26 insertions, 15 deletions
@@ -4299,8 +4299,7 @@ class IwyuAction : public ASTFrontendAction { CompilerInstance& compiler, // NOLINT llvm::StringRef /* dummy */) override { // Do this first thing after getting our hands on a CompilerInstance. - InitGlobals(&compiler.getSourceManager(), - &compiler.getPreprocessor().getHeaderSearchInfo()); + InitGlobals(compiler); auto* const preprocessor_consumer = new IwyuPreprocessorInfo(); compiler.getPreprocessor().addPPCallbacks( diff --git a/iwyu_globals.cc b/iwyu_globals.cc index b43a480..8f71160 100644 --- a/iwyu_globals.cc +++ b/iwyu_globals.cc @@ -33,7 +33,9 @@ #include "clang/AST/PrettyPrinter.h" #include "clang/Basic/FileManager.h" #include "clang/Basic/Version.h" +#include "clang/Frontend/CompilerInstance.h" #include "clang/Lex/HeaderSearch.h" +#include "clang/Lex/Preprocessor.h" using clang::DirectoryEntry; using std::make_pair; @@ -408,22 +410,30 @@ static vector<HeaderSearchPath> ComputeHeaderSearchPaths( return NormalizeHeaderSearchPaths(search_path_map); } -void InitGlobals(clang::SourceManager* sm, clang::HeaderSearch* header_search) { - CHECK_(sm && "InitGlobals() needs a non-nullptr SourceManager"); - source_manager = sm; +static CStdLib DeriveCStdLib(clang::CompilerInstance&) { + if (GlobalFlags().no_default_mappings) + return CStdLib::None; + return CStdLib::Glibc; +} + +static CXXStdLib DeriveCXXStdLib(clang::CompilerInstance&) { + if (GlobalFlags().no_default_mappings) + return CXXStdLib::None; + return CXXStdLib::Libstdcxx; +} + +void InitGlobals(clang::CompilerInstance& compiler) { + source_manager = &compiler.getSourceManager(); data_getter = new SourceManagerCharacterDataGetter(*source_manager); - vector<HeaderSearchPath> search_paths = - ComputeHeaderSearchPaths(header_search); + vector<HeaderSearchPath> search_paths = ComputeHeaderSearchPaths( + &compiler.getPreprocessor().getHeaderSearchInfo()); SetHeaderSearchPaths(search_paths); - CStdLib cstdlib = CStdLib::Glibc; - CXXStdLib cxxstdlib = CXXStdLib::Libstdcxx; - if (GlobalFlags().no_default_mappings) { - cstdlib = CStdLib::None; - cxxstdlib = CXXStdLib::None; - } - include_picker = - new IncludePicker(GlobalFlags().regex_dialect, cstdlib, cxxstdlib); + RegexDialect regex_dialect = GlobalFlags().regex_dialect; + CStdLib cstdlib = DeriveCStdLib(compiler); + CXXStdLib cxxstdlib = DeriveCXXStdLib(compiler); + include_picker = new IncludePicker(regex_dialect, cstdlib, cxxstdlib); + function_calls_full_use_cache = new FullUseCache; class_members_full_use_cache = new FullUseCache; diff --git a/iwyu_globals.h b/iwyu_globals.h index 84b613f..23b6b2f 100644 --- a/iwyu_globals.h +++ b/iwyu_globals.h @@ -16,6 +16,7 @@ #include <vector> // for vector namespace clang { +class CompilerInstance; class FileEntry; class HeaderSearch; class SourceManager; @@ -56,6 +57,7 @@ class OptionsParser { }; void InitGlobals(clang::SourceManager* sm, clang::HeaderSearch* header_search); +void InitGlobals(clang::CompilerInstance& compiler); // Can be called by tests -- doesn't need a SourceManager or // argc/argv. Note that GlobalSourceManager() and DefaultDataGetter() |