Skip to content

Commit e29e778

Browse files
Add GetIncludePaths() to fetch the include paths
1 parent 0e75241 commit e29e778

File tree

6 files changed

+89
-0
lines changed

6 files changed

+89
-0
lines changed

Diff for: include/clang/Interpreter/CppInterOp.h

+3
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,9 @@ namespace Cpp {
444444
/// Returns the resource-dir path (for headers).
445445
const char* GetResourceDir();
446446

447+
/// Get Include Paths
448+
void GetIncludePath(const char* dir, std::vector<std::string>& includePaths);
449+
447450
/// Secondary search path for headers, if not found using the
448451
/// GetResourceDir() function.
449452
void AddIncludePath(const char *dir);

Diff for: lib/Interpreter/CppInterOp.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -2530,6 +2530,10 @@ namespace Cpp {
25302530
getInterp().AddIncludePath(dir);
25312531
}
25322532

2533+
void GetIncludePath(const char* dir, std::vector<std::string>& Paths) {
2534+
getInterp().GetIncludePath(dir, Paths);
2535+
}
2536+
25332537
namespace {
25342538

25352539
class clangSilent {

Diff for: lib/Interpreter/CppInterOpInterpreter.h

+22
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,28 @@ class Interpreter {
383383
return AddIncludePaths(PathsStr, nullptr);
384384
}
385385

386+
///\brief Returns multiple include paths separated by a delimter.
387+
///
388+
///\param[in] includePaths - Store Path(s)
389+
///\param[in] PathsStr - Path(s)
390+
///\param[in] Delim - Delimiter to separate paths or NULL if a single path
391+
///
392+
void GetIncludePaths(std::vector<std::string>& includePaths,
393+
llvm::StringRef PathsStr, const char* Delim = ":") {
394+
const clang::CompilerInstance* CI = getCompilerInstance();
395+
clang::HeaderSearchOptions& HOpts =
396+
const_cast<clang::HeaderSearchOptions&>(CI->getHeaderSearchOpts());
397+
398+
Cpp::utils::GetIncludePaths(includePaths, PathsStr, HOpts, Delim);
399+
}
400+
401+
///\brief Returns a single include path (-I).
402+
///
403+
void GetIncludePath(llvm::StringRef PathsStr,
404+
std::vector<std::string>& includePaths) {
405+
return GetIncludePaths(includePaths, PathsStr, nullptr);
406+
}
407+
386408
CompilationResult loadLibrary(const std::string& filename, bool lookup) {
387409
DynamicLibraryManager* DLM = getDynamicLibraryManager();
388410
std::string canonicalLib;

Diff for: lib/Interpreter/Paths.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -449,5 +449,44 @@ void AddIncludePaths(llvm::StringRef PathStr,
449449
#undef DEBUG_TYPE
450450
}
451451

452+
void GetIncludePaths(
453+
std::vector<std::string>& includePaths, llvm::StringRef PathStr,
454+
clang::HeaderSearchOptions& HOpts,
455+
const char* Delim /* = Cpp::utils::platform::kEnvDelim */) {
456+
#define DEBUG_TYPE "GetIncludePaths"
457+
458+
llvm::SmallVector<llvm::StringRef, 10> Paths;
459+
if (Delim && *Delim)
460+
SplitPaths(PathStr, Paths, kAllowNonExistant, Delim, HOpts.Verbose);
461+
else
462+
Paths.push_back(PathStr);
463+
464+
// Avoid duplicates
465+
llvm::SmallVector<llvm::StringRef, 10> PathsChecked;
466+
for (llvm::StringRef Path : Paths) {
467+
bool Exists = false;
468+
for (const clang::HeaderSearchOptions::Entry& E : HOpts.UserEntries) {
469+
if ((Exists = E.Path == Path))
470+
break;
471+
}
472+
if (!Exists)
473+
PathsChecked.push_back(Path);
474+
includePaths.push_back((std::string)Path);
475+
}
476+
477+
const bool IsFramework = false;
478+
const bool IsSysRootRelative = true;
479+
for (llvm::StringRef Path : PathsChecked)
480+
HOpts.AddPath(Path, clang::frontend::Angled, IsFramework,
481+
IsSysRootRelative);
482+
483+
if (HOpts.Verbose) {
484+
LLVM_DEBUG(dbgs() << "Added include paths:\n");
485+
for (llvm::StringRef Path : PathsChecked)
486+
LLVM_DEBUG(dbgs() << " " << Path << "\n");
487+
}
488+
#undef DEBUG_TYPE
489+
}
490+
452491
} // namespace utils
453492
} // namespace Cpp

Diff for: lib/Interpreter/Paths.h

+13
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,19 @@ void CopyIncludePaths(const clang::HeaderSearchOptions& Opts,
158158
///
159159
void DumpIncludePaths(const clang::HeaderSearchOptions& Opts,
160160
llvm::raw_ostream& Out, bool WithSystem, bool WithFlags);
161+
162+
///\brief Get multiple include paths separated by a delimter into the
163+
/// given HeaderSearchOptions. This helps us to store the include paths in
164+
/// a vector, includePaths.
165+
///
166+
///\param[in] includePaths - Store the include paths
167+
///\param[in] PathStr - Path(s)
168+
///\param[in] HOpts - HeaderSearchOptions to add paths into
169+
///\param[in] Delim - Delimiter to separate paths or NULL if a single path
170+
///
171+
void GetIncludePaths(std::vector<std::string>& includePaths,
172+
llvm::StringRef PathStr, clang::HeaderSearchOptions& HOpts,
173+
const char* Delim = Cpp::utils::platform::kEnvDelim);
161174
} // namespace utils
162175
} // namespace Cpp
163176

Diff for: unittests/CppInterOp/InterpreterTest.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,11 @@ TEST(InterpreterTest, CreateInterpreter) {
8484
EXPECT_TRUE(Cpp::GetNamed("cpp17"));
8585
EXPECT_FALSE(Cpp::GetNamed("cppUnknown"));
8686
}
87+
88+
TEST(InterpreterTest, GetIncludePath) {
89+
std::vector <std::string> includePaths;
90+
const char* dir = Cpp::GetResourceDir();
91+
Cpp::AddIncludePath(dir);
92+
Cpp::GetIncludePath(dir, includePaths);
93+
EXPECT_TRUE(includePaths.size() > 0);
94+
}

0 commit comments

Comments
 (0)