Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for GetIncludePaths #178

Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
void GetIncludePath(const char* dir, std::vector<std::string>& includePaths);
void GetIncludePath(std::vector<std::string>& includePaths);

This function should only have a single output parameter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh okay, I will make the required changes.


/// 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();
auto& HOpts =
const_cast<clang::HeaderSearchOptions&>(CI->getHeaderSearchOpts());
Krishna-13-cyber marked this conversation as resolved.
Show resolved Hide resolved

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
27 changes: 27 additions & 0 deletions lib/Interpreter/Paths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,5 +449,32 @@
#undef DEBUG_TYPE
}

void GetIncludePaths(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: an assignment within an 'if' condition is bug-prone [bugprone-assignment-in-if-condition]

      if ((Exists = E.Path == Path))
           ^
Additional context

lib/Interpreter/Paths.cpp:450: if it should be an assignment, move it out of the 'if' condition

      if ((Exists = E.Path == Path))
           ^

lib/Interpreter/Paths.cpp:450: if it is meant to be an equality check, change '=' to '=='

      if ((Exists = E.Path == Path))
           ^

std::vector<std::string>& includePaths, llvm::StringRef PathStr,
clang::HeaderSearchOptions& HOpts,
const char* Delim /* = Cpp::utils::platform::kEnvDelim */) {
#define DEBUG_TYPE "GetIncludePaths"
const int val = 10;
llvm::SmallVector<llvm::StringRef, val> Paths;
if ((Delim != nullptr) && (*Delim != 0))
SplitPaths(PathStr, Paths, kAllowNonExistant, Delim, HOpts.Verbose);
else
Paths.push_back(PathStr);

// Avoid duplicates
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unused variable 'Path' [clang-diagnostic-unused-variable]

    for (llvm::StringRef Path : PathsChecked)
                         ^

for (llvm::StringRef Path : Paths) {
Krishna-13-cyber marked this conversation as resolved.
Show resolved Hide resolved
bool Exists = false;

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

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/Paths.cpp#L464-L466

Added lines #L464 - L466 were not covered by tests
for (const clang::HeaderSearchOptions::Entry& E : HOpts.UserEntries) {
if ((E.Path == Path))
Exists = true;
break;
}
if (!Exists) {
includePaths.push_back((std::string)Path);
Krishna-13-cyber marked this conversation as resolved.
Show resolved Hide resolved
Krishna-13-cyber marked this conversation as resolved.
Show resolved Hide resolved
}
}
#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);
}
Loading