Skip to content

Commit

Permalink
Add GetIncludePaths() to fetch the include paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Krishna-13-cyber committed Jan 5, 2024
1 parent 0e75241 commit e29e778
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/clang/Interpreter/CppInterOp.h
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,9 @@ namespace Cpp {
/// Returns the resource-dir path (for headers).
const char* GetResourceDir();

/// Get Include Paths
void GetIncludePath(const char* dir, std::vector<std::string>& includePaths);

/// Secondary search path for headers, if not found using the
/// GetResourceDir() function.
void AddIncludePath(const char *dir);
Expand Down
4 changes: 4 additions & 0 deletions lib/Interpreter/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2530,6 +2530,10 @@ namespace Cpp {
getInterp().AddIncludePath(dir);
}

void GetIncludePath(const char* dir, std::vector<std::string>& Paths) {
getInterp().GetIncludePath(dir, Paths);
}

namespace {

class clangSilent {
Expand Down
22 changes: 22 additions & 0 deletions lib/Interpreter/CppInterOpInterpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,28 @@ class Interpreter {
return AddIncludePaths(PathsStr, nullptr);
}

///\brief Returns multiple include paths separated by a delimter.
///
///\param[in] includePaths - Store Path(s)
///\param[in] PathsStr - Path(s)
///\param[in] Delim - Delimiter to separate paths or NULL if a single path
///
void GetIncludePaths(std::vector<std::string>& includePaths,
llvm::StringRef PathsStr, const char* Delim = ":") {
const clang::CompilerInstance* CI = getCompilerInstance();
clang::HeaderSearchOptions& HOpts =
const_cast<clang::HeaderSearchOptions&>(CI->getHeaderSearchOpts());

Cpp::utils::GetIncludePaths(includePaths, PathsStr, HOpts, Delim);
}

///\brief Returns a single include path (-I).
///
void GetIncludePath(llvm::StringRef PathsStr,
std::vector<std::string>& includePaths) {
return GetIncludePaths(includePaths, PathsStr, nullptr);
}

CompilationResult loadLibrary(const std::string& filename, bool lookup) {
DynamicLibraryManager* DLM = getDynamicLibraryManager();
std::string canonicalLib;
Expand Down
39 changes: 39 additions & 0 deletions lib/Interpreter/Paths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,5 +449,44 @@ void AddIncludePaths(llvm::StringRef PathStr,
#undef DEBUG_TYPE
}

void GetIncludePaths(
std::vector<std::string>& includePaths, llvm::StringRef PathStr,
clang::HeaderSearchOptions& HOpts,
const char* Delim /* = Cpp::utils::platform::kEnvDelim */) {
#define DEBUG_TYPE "GetIncludePaths"

llvm::SmallVector<llvm::StringRef, 10> Paths;
if (Delim && *Delim)
SplitPaths(PathStr, Paths, kAllowNonExistant, Delim, HOpts.Verbose);

Check warning on line 460 in lib/Interpreter/Paths.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/Paths.cpp#L460

Added line #L460 was not covered by tests
else
Paths.push_back(PathStr);

// Avoid duplicates
llvm::SmallVector<llvm::StringRef, 10> PathsChecked;
for (llvm::StringRef Path : Paths) {
bool Exists = false;
for (const clang::HeaderSearchOptions::Entry& E : HOpts.UserEntries) {
if ((Exists = E.Path == Path))
break;
}
if (!Exists)
PathsChecked.push_back(Path);

Check warning on line 473 in lib/Interpreter/Paths.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/Paths.cpp#L473

Added line #L473 was not covered by tests
includePaths.push_back((std::string)Path);
}

const bool IsFramework = false;
const bool IsSysRootRelative = true;
for (llvm::StringRef Path : PathsChecked)
HOpts.AddPath(Path, clang::frontend::Angled, IsFramework,

Check warning on line 480 in lib/Interpreter/Paths.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/Paths.cpp#L480

Added line #L480 was not covered by tests
IsSysRootRelative);

if (HOpts.Verbose) {
LLVM_DEBUG(dbgs() << "Added include paths:\n");
for (llvm::StringRef Path : PathsChecked)
LLVM_DEBUG(dbgs() << " " << Path << "\n");

Check warning on line 486 in lib/Interpreter/Paths.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/Paths.cpp#L484-L486

Added lines #L484 - L486 were not covered by tests
}
#undef DEBUG_TYPE
}

} // namespace utils
} // namespace Cpp
13 changes: 13 additions & 0 deletions lib/Interpreter/Paths.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,19 @@ void CopyIncludePaths(const clang::HeaderSearchOptions& Opts,
///
void DumpIncludePaths(const clang::HeaderSearchOptions& Opts,
llvm::raw_ostream& Out, bool WithSystem, bool WithFlags);

///\brief Get multiple include paths separated by a delimter into the
/// given HeaderSearchOptions. This helps us to store the include paths in
/// a vector, includePaths.
///
///\param[in] includePaths - Store the include paths
///\param[in] PathStr - Path(s)
///\param[in] HOpts - HeaderSearchOptions to add paths into
///\param[in] Delim - Delimiter to separate paths or NULL if a single path
///
void GetIncludePaths(std::vector<std::string>& includePaths,
llvm::StringRef PathStr, clang::HeaderSearchOptions& HOpts,
const char* Delim = Cpp::utils::platform::kEnvDelim);
} // namespace utils
} // namespace Cpp

Expand Down
8 changes: 8 additions & 0 deletions unittests/CppInterOp/InterpreterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,11 @@ TEST(InterpreterTest, CreateInterpreter) {
EXPECT_TRUE(Cpp::GetNamed("cpp17"));
EXPECT_FALSE(Cpp::GetNamed("cppUnknown"));
}

TEST(InterpreterTest, GetIncludePath) {
std::vector <std::string> includePaths;
const char* dir = Cpp::GetResourceDir();
Cpp::AddIncludePath(dir);
Cpp::GetIncludePath(dir, includePaths);
EXPECT_TRUE(includePaths.size() > 0);
}

0 comments on commit e29e778

Please sign in to comment.