Skip to content

Commit 6557d10

Browse files
authored
[interpreter] new function TInterpreter::SetIncludePath
Fixes https://its.cern.ch/jira/browse/ROOT-5149
1 parent d067fac commit 6557d10

File tree

8 files changed

+66
-11
lines changed

8 files changed

+66
-11
lines changed

core/meta/inc/TInterpreter.h

+1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ class TInterpreter : public TNamed {
137137
virtual ~TInterpreter() { }
138138

139139
virtual void AddIncludePath(const char *path) = 0;
140+
virtual void SetIncludePath(const char *path) = 0;
140141
virtual void *SetAutoLoadCallBack(void* /*cb*/) { return nullptr; }
141142
virtual void *GetAutoLoadCallBack() const { return nullptr; }
142143
virtual Int_t AutoLoad(const char *classname, Bool_t knowDictNotLoaded = kFALSE) = 0;

core/metacling/src/TCling.cxx

+18-9
Original file line numberDiff line numberDiff line change
@@ -1323,7 +1323,7 @@ static void RegisterPreIncludedHeaders(cling::Interpreter &clingInterp)
13231323
#ifndef R__WIN32
13241324
PreIncludes += "#include <cassert>\n";
13251325
#endif
1326-
PreIncludes += "using namespace std;\n";
1326+
PreIncludes += "using namespace std;\n";
13271327
clingInterp.declare(PreIncludes);
13281328
}
13291329

@@ -2683,6 +2683,23 @@ void TCling::AddIncludePath(const char *path)
26832683
fInterpreter->AddIncludePath(sPath.Data());
26842684
}
26852685

2686+
////////////////////////////////////////////////////////////////////////////////
2687+
/// \brief Replaces current list of directories with a single path in which the
2688+
/// interpreter looks for include files.
2689+
/// \param[in] path The path to the directory.
2690+
/// \note Only one path item can be specified at a time, i.e. "path1:path2" is
2691+
/// \b NOT supported.
2692+
/// \warning Only the path to the directory should be specified, without
2693+
/// prepending the \c -I prefix, i.e.
2694+
/// <tt>gCling->AddIncludePath("/path/to/my/includes")</tt>. If the
2695+
/// \c -I prefix is used it will be ignored.
2696+
void TCling::SetIncludePath(const char *path)
2697+
{
2698+
R__LOCKGUARD(gInterpreterMutex);
2699+
fInterpreter->ResetIncludePaths();
2700+
AddIncludePath(path);
2701+
}
2702+
26862703
////////////////////////////////////////////////////////////////////////////////
26872704
/// Visit all members over members, recursing over base classes.
26882705

@@ -3075,14 +3092,6 @@ void TCling::InspectMembers(TMemberInspector& insp, const void* obj,
30753092
} // loop over bases
30763093
}
30773094

3078-
////////////////////////////////////////////////////////////////////////////////
3079-
/// Check if constructor exited correctly, ie the instance is in a valid state
3080-
/// \return true if there is a compiler instance available, false otherwise
3081-
bool TCling::IsValid() const
3082-
{
3083-
return fInterpreter->getCI() != nullptr;
3084-
}
3085-
30863095
////////////////////////////////////////////////////////////////////////////////
30873096
/// Reset the interpreter internal state in case a previous action was not correctly
30883097
/// terminated.

core/metacling/src/TCling.h

+1
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ class TCling final : public TInterpreter {
195195
TCling(const char* name, const char* title, const char* const argv[], void *interpLibHandle);
196196

197197
void AddIncludePath(const char* path) final;
198+
void SetIncludePath(const char* path) final;
198199
void *GetAutoLoadCallBack() const final { return fAutoLoadCallBack; }
199200
void *SetAutoLoadCallBack(void* cb) final { void* prev = fAutoLoadCallBack; fAutoLoadCallBack = cb; return prev; }
200201
Int_t AutoLoad(const char *classname, Bool_t knowDictNotLoaded = kFALSE) final;

interpreter/cling/include/cling/Interpreter/Interpreter.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -438,12 +438,16 @@ namespace cling {
438438
///
439439
bool isUniqueName(llvm::StringRef name);
440440

441-
///\brief Adds multiple include paths separated by a delimter.
441+
///\brief Adds multiple include paths separated by a delimiter.
442442
///
443443
///\param[in] PathsStr - Path(s)
444444
///\param[in] Delim - Delimiter to separate paths or NULL if a single path
445445
///
446446
void AddIncludePaths(llvm::StringRef PathsStr, const char* Delim = ":");
447+
448+
///\brief Unsets preexisting include paths.
449+
///
450+
void ResetIncludePaths();
447451

448452
///\brief Adds a single include path (-I).
449453
///

interpreter/cling/lib/Interpreter/Interpreter.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,28 @@ namespace cling {
651651
if (m_CUDACompiler)
652652
m_CUDACompiler->getPTXInterpreter()->AddIncludePaths(PathStr, Delm);
653653
}
654+
655+
void Interpreter::ResetIncludePaths() {
656+
CompilerInstance* CI = getCI();
657+
HeaderSearchOptions& HOpts = CI->getHeaderSearchOpts();
658+
659+
Preprocessor& PP = CI->getPreprocessor();
660+
SourceManager& SM = PP.getSourceManager();
661+
FileManager& FM = SM.getFileManager();
662+
HeaderSearch& HSearch = PP.getHeaderSearchInfo();
663+
const bool isFramework = false;
664+
// Remove old entries from Preprocessor
665+
size_t Idx = HOpts.UserEntries.size();
666+
for (const size_t N = HOpts.UserEntries.size(); Idx < N; ++Idx) {
667+
const HeaderSearchOptions::Entry& E = HOpts.UserEntries[Idx];
668+
if (auto DE = FM.getOptionalDirectoryRef(E.Path))
669+
HSearch.RemoveSearchPath(DirectoryLookup(*DE, SrcMgr::C_User, isFramework),
670+
E.Group == frontend::Angled);
671+
}
672+
HOpts.ResetPaths();
673+
if (m_CUDACompiler)
674+
m_CUDACompiler->getPTXInterpreter()->ResetIncludePaths();
675+
}
654676

655677
void Interpreter::AddIncludePath(llvm::StringRef PathsStr) {
656678
return AddIncludePaths(PathsStr, nullptr);

interpreter/llvm-project/clang/include/clang/Lex/HeaderSearch.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,10 @@ class HeaderSearch {
375375

376376
/// Add an additional search path.
377377
void AddSearchPath(const DirectoryLookup &dir, bool isAngled);
378-
378+
379+
/// Remove search path.
380+
void RemoveSearchPath(const DirectoryLookup &dir, bool isAngled);
381+
379382
/// Add an additional system search path.
380383
void AddSystemSearchPath(const DirectoryLookup &dir) {
381384
SearchDirs.push_back(dir);

interpreter/llvm-project/clang/include/clang/Lex/HeaderSearchOptions.h

+5
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,11 @@ class HeaderSearchOptions {
284284
bool IsFramework, bool IgnoreSysRoot) {
285285
UserEntries.emplace_back(Path, Group, IsFramework, IgnoreSysRoot);
286286
}
287+
288+
/// ResetPaths - Removes all paths from the user entries list.
289+
void ResetPaths() {
290+
UserEntries.clear();
291+
}
287292

288293
/// AddSystemHeaderPrefix - Override whether \#include directives naming a
289294
/// path starting with \p Prefix should be considered as naming a system

interpreter/llvm-project/clang/lib/Lex/HeaderSearch.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,16 @@ void HeaderSearch::AddSearchPath(const DirectoryLookup &dir, bool isAngled) {
127127
SystemDirIdx++;
128128
}
129129

130+
void HeaderSearch::RemoveSearchPath(const DirectoryLookup &dir, bool isAngled) {
131+
auto position = std::find(SearchDirs.begin(), SearchDirs.end(), dir);
132+
auto idx = std::distance(SearchDirs.begin(), position);
133+
SearchDirs.erase(position);
134+
SearchDirsUsage.erase(SearchDirsUsage.begin() + idx);
135+
if (!isAngled)
136+
AngledDirIdx--;
137+
SystemDirIdx--;
138+
}
139+
130140
std::vector<bool> HeaderSearch::computeUserEntryUsage() const {
131141
std::vector<bool> UserEntryUsage(HSOpts->UserEntries.size());
132142
for (unsigned I = 0, E = SearchDirsUsage.size(); I < E; ++I) {

0 commit comments

Comments
 (0)