Skip to content

Implement GetIncludePaths #311

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

Merged
merged 8 commits into from
Aug 12, 2024

Conversation

Vipul-Cariappa
Copy link
Collaborator

Fixes #69

I was unsure if to return std::vector<std::string> or a single std::string with delimiter. I have gone with a single std::string with : as the delimiter, but it can be changed.


With the following changes in the cppyy library.

diff --git a/python/cppyy/__init__.py b/python/cppyy/__init__.py
index 53b7e92..441c34c 100644
--- a/python/cppyy/__init__.py
+++ b/python/cppyy/__init__.py
@@ -260,6 +260,11 @@ def add_include_path(path):
         raise OSError('No such directory: %s' % path)
     gbl.Cpp.AddIncludePath(path)
 
+def get_include_path():
+    """Get include paths available to Cling."""
+    path = gbl.Cpp.GetIncludePaths()
+    return path.split(":")
+
 def add_library_path(path):
     """Add a path to the library search paths available to Cling."""
     if not os.path.isdir(path):

We can get the include paths in Python

>>> import cppyy
>>> print(*cppyy.get_include_path(), sep="\n")
.
/home/vipul/Workspace/c/llvm-project/llvm/include
/home/vipul/Workspace/c/llvm-project/clang/include
/home/vipul/Workspace/c/llvm-project/build/include
/home/vipul/Workspace/c/llvm-project/build/tools/clang/include
/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14
/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/x86_64-redhat-linux
/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/backward
/home/vipul/Workspace/c/llvm-project/build/lib/clang/17/include
/usr/local/include
/usr/lib/gcc/x86_64-redhat-linux/14/../../../../x86_64-redhat-linux/include
/include
/usr/include
/home/vipul/Workspace/c/llvm-project/build/lib/clang/17/../../../../cling-src/tools/cling/include
/home/vipul/Workspace/c/llvm-project/build/lib/clang/17/../../../../cling-src/include
/home/vipul/Workspace/c/llvm-project/build/lib/clang/17/../../..//include
/home/vipul/Workspace/c/cppyy-backend-compiler-research/python/cppyy_backend/include
/home/vipul/micromamba/envs/compiler-research/include/python3.12
/home/vipul/micromamba/envs/compiler-research/lib/python3.12/site-packages/../../../include/python3.12
/home/vipul/micromamba/envs/compiler-research/include

@vgvassilev
Copy link
Contributor

Fixes #69

I was unsure if to return std::vector<std::string> or a single std::string with delimiter. I have gone with a single std::string with : as the delimiter, but it can be changed.

Probably we should go for a std::vector<std::string>& as a output parameter.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

clang-tidy made some suggestions

Copy link

codecov bot commented Jul 27, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 73.38%. Comparing base (34186d6) to head (5eb82e1).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #311      +/-   ##
==========================================
+ Coverage   72.54%   73.38%   +0.83%     
==========================================
  Files           8        8              
  Lines        2972     2979       +7     
==========================================
+ Hits         2156     2186      +30     
+ Misses        816      793      -23     
Files Coverage Δ
include/clang/Interpreter/CppInterOp.h 96.00% <ø> (ø)
lib/Interpreter/CppInterOp.cpp 79.12% <100.00%> (+0.06%) ⬆️
lib/Interpreter/CppInterOpInterpreter.h 76.25% <100.00%> (+0.30%) ⬆️

... and 1 file with indirect coverage changes

Files Coverage Δ
include/clang/Interpreter/CppInterOp.h 96.00% <ø> (ø)
lib/Interpreter/CppInterOp.cpp 79.12% <100.00%> (+0.06%) ⬆️
lib/Interpreter/CppInterOpInterpreter.h 76.25% <100.00%> (+0.30%) ⬆️

... and 1 file with indirect coverage changes

@Vipul-Cariappa
Copy link
Collaborator Author

Looking at the failing test, cling has Interpreter::GetIncludePaths function defined as void Interpreter::GetIncludePaths(llvm::SmallVectorImpl<std::string>& incpaths, bool withSystem, bool withFlags) (reference: definition of Interpreter::GetIncludePaths in cling). I guess we need to define Interpreter::GetIncludePaths using the same function signature in CppInterOp too.

That raises a question; How should I handle the withSystem and withFlags arguments? This is what cling does. Should I just copy that code?

I don't completely know/understand some of the options there.


Probably we should go for a std::vector<std::string>& as a output parameter.

Not sure if we can return a reference, because we are not storing it as a vector in the class. We could return std::vector<std::string&>.
We are getting the include paths directly from LLVM using HeaderSearchOptions class.

@vgvassilev
Copy link
Contributor

Looking at the failing test, cling has Interpreter::GetIncludePaths function defined as void Interpreter::GetIncludePaths(llvm::SmallVectorImpl<std::string>& incpaths, bool withSystem, bool withFlags) (reference: definition of Interpreter::GetIncludePaths in cling). I guess we need to define Interpreter::GetIncludePaths using the same function signature in CppInterOp too.

That raises a question; How should I handle the withSystem and withFlags arguments? This is what cling does. Should I just copy that code?

That's probably the easiest.

I don't completely know/understand some of the options there.

Probably we should go for a std::vector<std::string>& as a output parameter.

Not sure if we can return a reference, because we are not storing it as a vector in the class. We could return std::vector<std::string&>. We are getting the include paths directly from LLVM using HeaderSearchOptions class.

I meant having a signature like: void GetIncludePaths(std::vector<std::string>& IncludePaths) where we will store the result.

@Vipul-Cariappa
Copy link
Collaborator Author

@vgvassilev, please have a look at the recent changes. I have adapted the code directly from cling. The behavior matches cling's behavior.

With the following changes in cppyy

diff --git a/python/cppyy/__init__.py b/python/cppyy/__init__.py
index 53b7e92..d75f048 100644
--- a/python/cppyy/__init__.py
+++ b/python/cppyy/__init__.py
@@ -260,6 +260,12 @@ def add_include_path(path):
         raise OSError('No such directory: %s' % path)
     gbl.Cpp.AddIncludePath(path)
 
+def get_include_path():
+    """Get include paths available to Cling."""
+    paths = gbl.std.vector[gbl.std.string]()
+    gbl.Cpp.GetIncludePaths(paths)
+    return list(map(str, paths))
+
 def add_library_path(path):
     """Add a path to the library search paths available to Cling."""
     if not os.path.isdir(path):

You can use it in Python

>>> import cppyy
>>> cppyy.get_include_path()
['',
 '/home/vipul/Workspace/c/llvm-project/build/lib/clang/17/../../../../cling-src/tools/cling/include',
 '/home/vipul/Workspace/c/llvm-project/build/lib/clang/17/../../../../cling-src/include',
 '/home/vipul/Workspace/c/llvm-project/build/lib/clang/17/../../..//include',
 '/home/vipul/Workspace/c/cppyy-backend-compiler-research/python/cppyy_backend/include',
 '/home/vipul/micromamba/envs/compiler-research/include/python3.12',
 '/home/vipul/micromamba/envs/compiler-research/lib/python3.12/site-packages/../../../include/python3.12',
 '/home/vipul/micromamba/envs/compiler-research/include']

@vgvassilev
Copy link
Contributor

This looks reasonable. We should add a unit test.

Copy link
Contributor

clang-tidy review says "All clean, LGTM! 👍"

1 similar comment
Copy link
Contributor

clang-tidy review says "All clean, LGTM! 👍"

@@ -113,6 +113,12 @@ TEST(InterpreterTest, DetectSystemCompilerIncludePaths) {
EXPECT_FALSE(includes.empty());
}

TEST(InterpreterTest, GetIncludePaths) {
std::vector<std::string> includes;
Copy link
Contributor

Choose a reason for hiding this comment

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

We should also test the separate flags and make sure the output is expected.


llvm::SmallVector<std::string> includes(1);

I->GetIncludePaths(includes, /*withSystem=*/false, /*withFlags=*/false);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Please note the difference in the signature of GetIncludePaths in include/clang/Interpreter/CppInterOp.h: void GetIncludePaths(std::vector<std::string>& IncludePaths) and void Interpreter::GetIncludePaths(llvm::SmallVectorImpl<std::string>& incpaths, bool withSystem, bool withFlags) in lib/Interpreter/CppInterOpInterpreter.h.

Should both of these functions have the same signature?
While retrieving include path from Python using cppyy. The user may also want to get the system path and flags, but cannot with the current implementation.

Copy link
Contributor

Choose a reason for hiding this comment

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

The Interpreter::GetIncludePaths should be hidden for the user. So I'd not worry about that.

Copy link
Contributor

@vgvassilev vgvassilev left a comment

Choose a reason for hiding this comment

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

LGTM!


llvm::SmallVector<std::string> includes(1);

I->GetIncludePaths(includes, /*withSystem=*/false, /*withFlags=*/false);
Copy link
Contributor

Choose a reason for hiding this comment

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

The Interpreter::GetIncludePaths should be hidden for the user. So I'd not worry about that.

Copy link
Contributor

github-actions bot commented Aug 5, 2024

clang-tidy review says "All clean, LGTM! 👍"

@vgvassilev
Copy link
Contributor

@Vipul-Cariappa, looks like the failures are due to some mismatch between the cling and clang-repl implementation. Maybe that's what you were trying to say in your previous comments. Can you look at failures?

@mcbarton
Copy link
Collaborator

mcbarton commented Aug 5, 2024

@Vipul-Cariappa can you rebase this PR, so you can see if any changes are needed to be compatible with llvm 19?

Copy link
Contributor

github-actions bot commented Aug 5, 2024

clang-tidy review says "All clean, LGTM! 👍"

@Vipul-Cariappa
Copy link
Collaborator Author

@vgvassilev, I believe the latest commit should fix the failing CI related to cling.
But clang-REPL LLVM 19 test also failed. Not sure if it's due to my changes.
Maybe it won't fail in the next run.

@aaronj0
Copy link
Collaborator

aaronj0 commented Aug 11, 2024

@vgvassilev, I believe the latest commit should fix the failing CI related to cling. But clang-REPL LLVM 19 test also failed. Not sure if it's due to my changes. Maybe it won't fail in the next run.

I wouldn't worry about that, It's probably valgrind complaining about clang19 on the cppyy tests(failing on master too) Has been an issue for a while now that I will update with a fix soon

Copy link
Contributor

clang-tidy review says "All clean, LGTM! 👍"

@aaronj0
Copy link
Collaborator

aaronj0 commented Aug 11, 2024

@Vipul-Cariappa A fix has been patched. I have rerun the failing job, can you rebase into a single commit once they are green

@vgvassilev vgvassilev merged commit bebebcd into compiler-research:main Aug 12, 2024
59 checks passed
@Vipul-Cariappa Vipul-Cariappa deleted the GetIncludePaths branch September 28, 2024 04:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement GetIncludePaths
4 participants