diff --git a/include/clang/Interpreter/CppInterOp.h b/include/clang/Interpreter/CppInterOp.h index 7e1d56d07..1ef734551 100644 --- a/include/clang/Interpreter/CppInterOp.h +++ b/include/clang/Interpreter/CppInterOp.h @@ -444,6 +444,9 @@ namespace Cpp { /// Returns the resource-dir path (for headers). const char* GetResourceDir(); + /// Get Include Paths + void GetIncludePath(std::vector& includePaths); + /// Secondary search path for headers, if not found using the /// GetResourceDir() function. void AddIncludePath(const char *dir); diff --git a/lib/Interpreter/CppInterOp.cpp b/lib/Interpreter/CppInterOp.cpp index 9a0c21e44..bdd5312b3 100644 --- a/lib/Interpreter/CppInterOp.cpp +++ b/lib/Interpreter/CppInterOp.cpp @@ -2530,6 +2530,10 @@ namespace Cpp { getInterp().AddIncludePath(dir); } + void GetIncludePath(std::vector& Paths) { + getInterp().GetIncludePath(Paths); + } + namespace { class clangSilent { diff --git a/lib/Interpreter/CppInterOpInterpreter.h b/lib/Interpreter/CppInterOpInterpreter.h index 566c332b9..7ee844e1e 100644 --- a/lib/Interpreter/CppInterOpInterpreter.h +++ b/lib/Interpreter/CppInterOpInterpreter.h @@ -347,6 +347,10 @@ class Interpreter { const_cast(this)->getDynamicLibraryManager()); } + /// @brief As a interface to store paths added in AddIncludePaths + std::vector include; + /// + ///\brief Adds multiple include paths separated by a delimter. /// ///\param[in] PathsStr - Path(s) @@ -359,7 +363,7 @@ class Interpreter { // Save the current number of entries size_t Idx = HOpts.UserEntries.size(); - Cpp::utils::AddIncludePaths(PathsStr, HOpts, Delim); + Cpp::utils::GetIncludePaths(include, PathsStr, HOpts, Delim); clang::Preprocessor& PP = CI->getPreprocessor(); clang::SourceManager& SM = PP.getSourceManager(); @@ -383,6 +387,17 @@ class Interpreter { return AddIncludePaths(PathsStr, nullptr); } + ///\brief Stores include paths. + ///\param[in] includePaths - Store Path(s) + /// + void GetIncludePaths(std::vector& includePaths) { + includePaths = std::move(include); + } + + void GetIncludePath(std::vector& includePaths) { + return GetIncludePaths(includePaths); + } + CompilationResult loadLibrary(const std::string& filename, bool lookup) { DynamicLibraryManager* DLM = getDynamicLibraryManager(); std::string canonicalLib; diff --git a/lib/Interpreter/Paths.cpp b/lib/Interpreter/Paths.cpp index 1913f4540..4625eb943 100644 --- a/lib/Interpreter/Paths.cpp +++ b/lib/Interpreter/Paths.cpp @@ -434,7 +434,6 @@ void AddIncludePaths(llvm::StringRef PathStr, if (!Exists) PathsChecked.push_back(Path); } - const bool IsFramework = false; const bool IsSysRootRelative = true; for (llvm::StringRef Path : PathsChecked) @@ -449,5 +448,32 @@ void AddIncludePaths(llvm::StringRef PathStr, #undef DEBUG_TYPE } +void GetIncludePaths( + std::vector& includePaths, llvm::StringRef PathStr, + clang::HeaderSearchOptions& HOpts, + const char* Delim /* = Cpp::utils::platform::kEnvDelim */) { +#define DEBUG_TYPE "GetIncludePaths" + + const int val = 10; + llvm::SmallVector Paths; + if ((Delim != nullptr) && (*Delim != 0)) + SplitPaths(PathStr, Paths, kAllowNonExistant, Delim, HOpts.Verbose); + else + Paths.push_back(PathStr); + + // Avoid duplicates + for (llvm::StringRef Path : Paths) { + bool Exists = false; + for (const clang::HeaderSearchOptions::Entry& E : HOpts.UserEntries) { + if ((E.Path == Path)) + Exists = true; + break; + } + if (!Exists) + includePaths.push_back((std::string)Path); + } +#undef DEBUG_TYPE +} + } // namespace utils } // namespace Cpp diff --git a/lib/Interpreter/Paths.h b/lib/Interpreter/Paths.h index 8b88f560f..589cf4fe8 100644 --- a/lib/Interpreter/Paths.h +++ b/lib/Interpreter/Paths.h @@ -124,6 +124,19 @@ bool LookForFile(const std::vector& Args, std::string& File, void AddIncludePaths(llvm::StringRef PathStr, clang::HeaderSearchOptions& HOpts, const char* Delim = Cpp::utils::platform::kEnvDelim); +///\brief Store multiple include paths separated by a delimter into the +/// given HeaderSearchOptions. This stores the paths but does no further +/// processing. +/// +///\param[in] includePaths - To store paths added in HeaderSearchOptions +///\param[in] PathStr - Path(s) +///\param[in] Opts - HeaderSearchOptions to add paths into +///\param[in] Delim - Delimiter to separate paths or NULL if a single path +/// +void GetIncludePaths(std::vector& includePaths, + llvm::StringRef PathStr, clang::HeaderSearchOptions& HOpts, + const char* Delim = Cpp::utils::platform::kEnvDelim); + ///\brief Write to cling::errs that directory does not exist in a format /// matching what 'clang -v' would do /// diff --git a/unittests/CppInterOp/InterpreterTest.cpp b/unittests/CppInterOp/InterpreterTest.cpp index e64cb8d7c..588d3cd05 100644 --- a/unittests/CppInterOp/InterpreterTest.cpp +++ b/unittests/CppInterOp/InterpreterTest.cpp @@ -84,3 +84,11 @@ TEST(InterpreterTest, CreateInterpreter) { EXPECT_TRUE(Cpp::GetNamed("cpp17")); EXPECT_FALSE(Cpp::GetNamed("cppUnknown")); } + +TEST(InterpreterTest, GetIncludePath) { + std::vector includePaths {}; + const char* dir = Cpp::GetResourceDir(); + Cpp::AddIncludePath(dir); + Cpp::GetIncludePath(includePaths); + EXPECT_TRUE(includePaths.size() > 0); +} \ No newline at end of file