[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
Fred Fu via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 6 10:40:40 PST 2023
https://github.com/capfredf updated https://github.com/llvm/llvm-project/pull/67349
>From 582f652f35d813a86790bc62473a0efc4560ae9a Mon Sep 17 00:00:00 2001
From: Fred Fu <moonsolo at gmail.com>
Date: Tue, 29 Aug 2023 11:56:59 -0400
Subject: [PATCH 1/9] [ClangRepl] Type Directed Code Completion
---
.../clang/Interpreter/CodeCompletion.h | 11 +-
clang/lib/Interpreter/CodeCompletion.cpp | 189 ++++++++++++++-
clang/tools/clang-repl/ClangRepl.cpp | 25 +-
.../Interpreter/CodeCompletionTest.cpp | 220 +++++++++++++++++-
4 files changed, 405 insertions(+), 40 deletions(-)
diff --git a/clang/include/clang/Interpreter/CodeCompletion.h b/clang/include/clang/Interpreter/CodeCompletion.h
index 9adcdf0dc3afac6..83f665b7a2db944 100644
--- a/clang/include/clang/Interpreter/CodeCompletion.h
+++ b/clang/include/clang/Interpreter/CodeCompletion.h
@@ -23,8 +23,13 @@ namespace clang {
class CodeCompletionResult;
class CompilerInstance;
-void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content,
- unsigned Line, unsigned Col, const CompilerInstance *ParentCI,
- std::vector<std::string> &CCResults);
+struct ReplCodeCompletion {
+ ReplCodeCompletion() = default;
+ std::string Prefix;
+ void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content,
+ unsigned Line, unsigned Col,
+ const CompilerInstance *ParentCI,
+ std::vector<std::string> &CCResults);
+};
} // namespace clang
#endif
diff --git a/clang/lib/Interpreter/CodeCompletion.cpp b/clang/lib/Interpreter/CodeCompletion.cpp
index c40e11b9d1ece0a..0eb493d1b894b39 100644
--- a/clang/lib/Interpreter/CodeCompletion.cpp
+++ b/clang/lib/Interpreter/CodeCompletion.cpp
@@ -12,6 +12,7 @@
#include "clang/Interpreter/CodeCompletion.h"
#include "clang/AST/ASTImporter.h"
+#include "clang/AST/DeclLookups.h"
#include "clang/AST/DeclarationName.h"
#include "clang/AST/ExternalASTSource.h"
#include "clang/Basic/IdentifierTable.h"
@@ -23,6 +24,8 @@
#include "clang/Sema/CodeCompleteConsumer.h"
#include "clang/Sema/CodeCompleteOptions.h"
#include "clang/Sema/Sema.h"
+#include "llvm/Support/Debug.h"
+#define DEBUG_TYPE "REPLCC"
namespace clang {
@@ -37,12 +40,22 @@ clang::CodeCompleteOptions getClangCompleteOpts() {
return Opts;
}
+class CodeCompletionSubContext {
+public:
+ virtual ~CodeCompletionSubContext(){};
+ virtual void HandleCodeCompleteResults(class Sema &S,
+ CodeCompletionResult *InResults,
+ unsigned NumResults,
+ std::vector<std::string> &Results) = 0;
+};
+
class ReplCompletionConsumer : public CodeCompleteConsumer {
public:
- ReplCompletionConsumer(std::vector<std::string> &Results)
+ ReplCompletionConsumer(std::vector<std::string> &Results,
+ ReplCodeCompletion &CC)
: CodeCompleteConsumer(getClangCompleteOpts()),
CCAllocator(std::make_shared<GlobalCodeCompletionAllocator>()),
- CCTUInfo(CCAllocator), Results(Results){};
+ CCTUInfo(CCAllocator), Results(Results), CC(CC) {}
void ProcessCodeCompleteResults(class Sema &S, CodeCompletionContext Context,
CodeCompletionResult *InResults,
@@ -56,26 +69,135 @@ class ReplCompletionConsumer : public CodeCompleteConsumer {
std::shared_ptr<GlobalCodeCompletionAllocator> CCAllocator;
CodeCompletionTUInfo CCTUInfo;
std::vector<std::string> &Results;
+ ReplCodeCompletion &CC;
+};
+
+class CompletionContextHanndler {
+protected:
+ CodeCompletionContext CCC;
+ std::vector<std::string> &Results;
+
+public:
+ CompletionContextHanndler(CodeCompletionContext CCC,
+ std::vector<std::string> &Results)
+ : CCC(CCC), Results(Results) {}
+ virtual void handleDeclaration(const CodeCompletionResult &Result) {}
+ virtual void handleKeyword(const CodeCompletionResult &Result) {}
+ virtual void handlePattern(const CodeCompletionResult &Result) {}
+ virtual void handleMacro(const CodeCompletionResult &Result) {}
+};
+
+class DotMemberAccessHandler : public CompletionContextHanndler {
+public:
+ DotMemberAccessHandler(CodeCompletionContext CCC,
+ std::vector<std::string> &Results)
+ : CompletionContextHanndler(CCC, Results) {}
+ void handleDeclaration(const CodeCompletionResult &Result) override {
+ if (auto *ID = Result.Declaration->getIdentifier()) {
+ if (const auto *Fun = llvm::dyn_cast<CXXMethodDecl>(Result.Declaration)) {
+ if (Fun->getParent()->getCanonicalDecl() ==
+ CCC.getBaseType()->getAsCXXRecordDecl()->getCanonicalDecl()) {
+ LLVM_DEBUG(llvm::dbgs() << "[In HandleCodeCompleteDOT] Name : "
+ << ID->getName() << "\n");
+ Results.push_back(ID->getName().str());
+ }
+ }
+ }
+ }
+};
+
+class DefaultAccessHandler : public CompletionContextHanndler {
+private:
+ Sema &S;
+
+public:
+ DefaultAccessHandler(Sema &S, CodeCompletionContext CCC,
+ std::vector<std::string> &Results)
+ : CompletionContextHanndler(CCC, Results), S(S) {}
+ void handleDeclaration(const CodeCompletionResult &Result) override {
+ auto PreferredType = CCC.getPreferredType();
+ if (!PreferredType.isNull()) {
+ if (auto *VD = dyn_cast<VarDecl>(Result.Declaration)) {
+ auto ArgumentType = VD->getType();
+ if (PreferredType->isReferenceType()) {
+ QualType RT =
+ PreferredType->castAs<ReferenceType>()->getPointeeType();
+ Sema::ReferenceConversions RefConv;
+ Sema::ReferenceCompareResult RefRelationship =
+ S.CompareReferenceRelationship(SourceLocation(), RT, ArgumentType,
+ &RefConv);
+ switch (RefRelationship) {
+ case Sema::Ref_Compatible:
+ case Sema::Ref_Related:
+ Results.push_back(VD->getName().str());
+ break;
+ case Sema::Ref_Incompatible:
+ break;
+ }
+ } else if (S.Context.hasSameType(ArgumentType, PreferredType)) {
+ Results.push_back(VD->getName().str());
+ }
+ }
+ } else
+ Results.push_back(Result.Declaration->getName().str());
+ }
+
+ void handleKeyword(const CodeCompletionResult &Result) override {
+ auto Prefix = S.getPreprocessor().getCodeCompletionFilter();
+ // add keyword to the completion results only if we are in a type-aware
+ // situation.
+ if (!CCC.getBaseType().isNull() || !CCC.getPreferredType().isNull())
+ return;
+ if (StringRef(Result.Keyword).startswith(Prefix))
+ Results.push_back(Result.Keyword);
+ }
};
void ReplCompletionConsumer::ProcessCodeCompleteResults(
class Sema &S, CodeCompletionContext Context,
CodeCompletionResult *InResults, unsigned NumResults) {
- for (unsigned I = 0; I < NumResults; ++I) {
+
+ // auto& a = S.Context.Idents.get("f1");
+ // LLVM_DEBUG(llvm::dbgs() << "\n FFFFF111111 : " << a.getName() << "\n" );
+ auto Prefix = S.getPreprocessor().getCodeCompletionFilter();
+ CC.Prefix = Prefix;
+
+ std::unique_ptr<CompletionContextHanndler> CCH;
+
+ switch (Context.getKind()) {
+ case CodeCompletionContext::CCC_DotMemberAccess:
+ CCH.reset(new DotMemberAccessHandler(Context, this->Results));
+ break;
+ default:
+ CCH.reset(new DefaultAccessHandler(S, Context, this->Results));
+ };
+
+ for (unsigned I = 0; I < NumResults; I++) {
auto &Result = InResults[I];
switch (Result.Kind) {
case CodeCompletionResult::RK_Declaration:
- if (auto *ID = Result.Declaration->getIdentifier()) {
- Results.push_back(ID->getName().str());
+ if (Result.Hidden) {
+ break;
+ }
+ if (!Result.Declaration->getDeclName().isIdentifier() ||
+ !Result.Declaration->getName().startswith(Prefix)) {
+ break;
}
+ CCH->handleDeclaration(Result);
break;
case CodeCompletionResult::RK_Keyword:
- Results.push_back(Result.Keyword);
+ CCH->handleKeyword(Result);
+ break;
+ case CodeCompletionResult::RK_Macro:
+ CCH->handleMacro(Result);
break;
- default:
+ case CodeCompletionResult::RK_Pattern:
+ CCH->handlePattern(Result);
break;
}
}
+
+ std::sort(Results.begin(), Results.end());
}
class IncrementalSyntaxOnlyAction : public SyntaxOnlyAction {
@@ -101,6 +223,7 @@ class ExternalSource : public clang::ExternalASTSource {
ASTContext &ParentASTCtxt, FileManager &ParentFM);
bool FindExternalVisibleDeclsByName(const DeclContext *DC,
DeclarationName Name) override;
+ // void CompleteType(TagDecl *Tag) override;
void
completeVisibleDeclsMap(const clang::DeclContext *childDeclContext) override;
};
@@ -118,6 +241,16 @@ void IncrementalSyntaxOnlyAction::ExecuteAction() {
CI.getASTContext().getTranslationUnitDecl()->setHasExternalVisibleStorage(
true);
+ // Load all external decls into current context. Under the hood, it calls
+ // ExternalSource::completeVisibleDeclsMap, which make all decls on the redecl
+ // chain visible.
+ //
+ // This is crucial to code completion on dot members, since a bound variable
+ // before "." would be otherwise treated out-of-scope.
+ //
+ // clang-repl> Foo f1;
+ // clang-repl> f1.<tab>
+ CI.getASTContext().getTranslationUnitDecl()->lookups();
SyntaxOnlyAction::ExecuteAction();
}
@@ -134,6 +267,7 @@ ExternalSource::ExternalSource(ASTContext &ChildASTCtxt, FileManager &ChildFM,
bool ExternalSource::FindExternalVisibleDeclsByName(const DeclContext *DC,
DeclarationName Name) {
+
IdentifierTable &ParentIdTable = ParentASTCtxt.Idents;
auto ParentDeclName =
@@ -166,8 +300,37 @@ void ExternalSource::completeVisibleDeclsMap(
SetExternalVisibleDeclsForName(ChildDeclContext,
importedNamedDecl->getDeclName(),
importedNamedDecl);
- }
+ if (auto *Record =
+ llvm::dyn_cast<CXXRecordDecl>(importedNamedDecl)) {
+ if (auto Err = Importer->ImportDefinition(Decl))
+ consumeError(std::move(Err));
+ // LLVM_DEBUG(llvm::dbgs() << "\nHello :\n");
+ // Record->getTypeForDecl()->dump();
+ Record->setHasLoadedFieldsFromExternalStorage(true);
+ // auto ToOrErr = Importer->Import(->getTypeForDecl());
+ // if (!ToOrErr) {
+ // consumeError(std::move(ToOrErr.takeError()));
+ // }
+ LLVM_DEBUG(
+ llvm::dbgs()
+ << "\nCXXRecrod : " << Record->getName() << " size(methods): "
+ << std::distance(Record->method_begin(), Record->method_end())
+ << " has def?: " << Record->hasDefinition()
+ << " # (methods): "
+ << std::distance(Record->getDefinition()->method_begin(),
+ Record->getDefinition()->method_end())
+ << "\n");
+ for (auto *Meth : Record->methods()) {
+ SetExternalVisibleDeclsForName(ChildDeclContext,
+ Meth->getDeclName(), Meth);
+ // if (Meth->getDeclName().isIdentifier()) {
+ // LLVM_DEBUG(llvm::dbgs() << "CXXRecrod Method: " <<
+ // Meth->getName() << "\n");
+ // }
+ }
+ }
+ }
} else {
llvm::consumeError(DeclOrErr.takeError());
}
@@ -177,11 +340,13 @@ void ExternalSource::completeVisibleDeclsMap(
}
}
-void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content,
- unsigned Line, unsigned Col, const CompilerInstance *ParentCI,
- std::vector<std::string> &CCResults) {
+void ReplCodeCompletion::codeComplete(CompilerInstance *InterpCI,
+ llvm::StringRef Content, unsigned Line,
+ unsigned Col,
+ const CompilerInstance *ParentCI,
+ std::vector<std::string> &CCResults) {
auto DiagOpts = DiagnosticOptions();
- auto consumer = ReplCompletionConsumer(CCResults);
+ auto consumer = ReplCompletionConsumer(CCResults, *this);
auto diag = InterpCI->getDiagnosticsPtr();
std::unique_ptr<ASTUnit> AU(ASTUnit::LoadFromCompilerInvocationAction(
diff --git a/clang/tools/clang-repl/ClangRepl.cpp b/clang/tools/clang-repl/ClangRepl.cpp
index 5663c2c5a6c9285..fba4b88c9c7dc70 100644
--- a/clang/tools/clang-repl/ClangRepl.cpp
+++ b/clang/tools/clang-repl/ClangRepl.cpp
@@ -15,6 +15,8 @@
#include "clang/Frontend/FrontendDiagnostic.h"
#include "clang/Interpreter/CodeCompletion.h"
#include "clang/Interpreter/Interpreter.h"
+#include "clang/Lex/Preprocessor.h"
+#include "clang/Sema/Sema.h"
#include "llvm/ExecutionEngine/Orc/LLJIT.h"
#include "llvm/LineEditor/LineEditor.h"
@@ -123,22 +125,15 @@ ReplListCompleter::operator()(llvm::StringRef Buffer, size_t Pos,
return {};
}
-
- codeComplete(
- const_cast<clang::CompilerInstance *>((*Interp)->getCompilerInstance()),
- Buffer, Lines, Pos + 1, MainInterp.getCompilerInstance(), Results);
-
- size_t space_pos = Buffer.rfind(" ");
- llvm::StringRef Prefix;
- if (space_pos == llvm::StringRef::npos) {
- Prefix = Buffer;
- } else {
- Prefix = Buffer.substr(space_pos + 1);
- }
-
+ auto *MainCI =
+ const_cast<clang::CompilerInstance *>((*Interp)->getCompilerInstance());
+ auto CC = clang::ReplCodeCompletion();
+ CC.codeComplete(MainCI, Buffer, Lines, Pos + 1,
+ MainInterp.getCompilerInstance(), Results);
for (auto c : Results) {
- if (c.find(Prefix) == 0)
- Comps.push_back(llvm::LineEditor::Completion(c.substr(Prefix.size()), c));
+ if (c.find(CC.Prefix) == 0)
+ Comps.push_back(
+ llvm::LineEditor::Completion(c.substr(CC.Prefix.size()), c));
}
return Comps;
}
diff --git a/clang/unittests/Interpreter/CodeCompletionTest.cpp b/clang/unittests/Interpreter/CodeCompletionTest.cpp
index 8f5f3545029d087..918b4e8167eb73c 100644
--- a/clang/unittests/Interpreter/CodeCompletionTest.cpp
+++ b/clang/unittests/Interpreter/CodeCompletionTest.cpp
@@ -1,7 +1,9 @@
#include "clang/Interpreter/CodeCompletion.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Interpreter/Interpreter.h"
+#include "clang/Lex/Preprocessor.h"
#include "clang/Sema/CodeCompleteConsumer.h"
+#include "clang/Sema/Sema.h"
#include "llvm/LineEditor/LineEditor.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/raw_ostream.h"
@@ -19,7 +21,7 @@ static std::unique_ptr<Interpreter> createInterpreter() {
}
static std::vector<std::string> runComp(clang::Interpreter &MainInterp,
- llvm::StringRef Prefix,
+ llvm::StringRef Input,
llvm::Error &ErrR) {
auto CI = CB.CreateCpp();
if (auto Err = CI.takeError()) {
@@ -37,16 +39,15 @@ static std::vector<std::string> runComp(clang::Interpreter &MainInterp,
std::vector<std::string> Results;
std::vector<std::string> Comps;
-
- codeComplete(
- const_cast<clang::CompilerInstance *>((*Interp)->getCompilerInstance()),
- Prefix, /* Lines */ 1, Prefix.size(), MainInterp.getCompilerInstance(),
- Results);
+ auto *MainCI =
+ const_cast<clang::CompilerInstance *>((*Interp)->getCompilerInstance());
+ auto CC = ReplCodeCompletion();
+ CC.codeComplete(MainCI, Input, /* Lines */ 1, Input.size() + 1,
+ MainInterp.getCompilerInstance(), Results);
for (auto Res : Results)
- if (Res.find(Prefix) == 0)
+ if (Res.find(CC.Prefix) == 0)
Comps.push_back(Res);
-
return Comps;
}
@@ -62,8 +63,9 @@ TEST(CodeCompletionTest, Sanity) {
}
auto Err = llvm::Error::success();
auto comps = runComp(*Interp, "f", Err);
- EXPECT_EQ((size_t)2, comps.size()); // foo and float
- EXPECT_EQ(comps[0], std::string("foo"));
+ EXPECT_EQ((size_t)2, comps.size()); // float and foo
+ EXPECT_EQ(comps[0], std::string("float"));
+ EXPECT_EQ(comps[1], std::string("foo"));
EXPECT_EQ((bool)Err, false);
}
@@ -110,4 +112,202 @@ TEST(CodeCompletionTest, CompFunDeclsNoError) {
EXPECT_EQ((bool)Err, false);
}
+TEST(CodeCompletionTest, TypedDirected) {
+ auto Interp = createInterpreter();
+ if (auto R = Interp->ParseAndExecute("int application = 12;")) {
+ consumeError(std::move(R));
+ return;
+ }
+ if (auto R = Interp->ParseAndExecute("char apple = '2';")) {
+ consumeError(std::move(R));
+ return;
+ }
+ if (auto R = Interp->ParseAndExecute("void add(int &SomeInt){}")) {
+ consumeError(std::move(R));
+ return;
+ }
+ {
+ auto Err = llvm::Error::success();
+ auto comps = runComp(*Interp, std::string("add("), Err);
+ EXPECT_EQ((size_t)1, comps.size());
+ EXPECT_EQ((bool)Err, false);
+ }
+
+ if (auto R = Interp->ParseAndExecute("int banana = 42;")) {
+ consumeError(std::move(R));
+ return;
+ }
+
+ {
+ auto Err = llvm::Error::success();
+ auto comps = runComp(*Interp, std::string("add("), Err);
+ EXPECT_EQ((size_t)2, comps.size());
+ EXPECT_EQ(comps[0], "application");
+ EXPECT_EQ(comps[1], "banana");
+ EXPECT_EQ((bool)Err, false);
+ }
+
+ {
+ auto Err = llvm::Error::success();
+ auto comps = runComp(*Interp, std::string("add(b"), Err);
+ EXPECT_EQ((size_t)1, comps.size());
+ EXPECT_EQ(comps[0], "banana");
+ EXPECT_EQ((bool)Err, false);
+ }
+}
+
+TEST(CodeCompletionTest, SanityClasses) {
+ auto Interp = createInterpreter();
+ if (auto R = Interp->ParseAndExecute("struct Apple{};")) {
+ consumeError(std::move(R));
+ return;
+ }
+ if (auto R = Interp->ParseAndExecute("void takeApple(Apple &a1){}")) {
+ consumeError(std::move(R));
+ return;
+ }
+ if (auto R = Interp->ParseAndExecute("Apple a1;")) {
+ consumeError(std::move(R));
+ return;
+ }
+ if (auto R = Interp->ParseAndExecute("void takeAppleCopy(Apple a1){}")) {
+ consumeError(std::move(R));
+ return;
+ }
+
+ {
+ auto Err = llvm::Error::success();
+ auto comps = runComp(*Interp, "takeApple(", Err);
+ EXPECT_EQ((size_t)1, comps.size());
+ EXPECT_EQ(comps[0], std::string("a1"));
+ EXPECT_EQ((bool)Err, false);
+ }
+ {
+ auto Err = llvm::Error::success();
+ auto comps = runComp(*Interp, std::string("takeAppleCopy("), Err);
+ EXPECT_EQ((size_t)1, comps.size());
+ EXPECT_EQ(comps[0], std::string("a1"));
+ EXPECT_EQ((bool)Err, false);
+ }
+}
+
+TEST(CodeCompletionTest, SubClassing) {
+ auto Interp = createInterpreter();
+ if (auto R = Interp->ParseAndExecute("struct Fruit {};")) {
+ consumeError(std::move(R));
+ return;
+ }
+ if (auto R = Interp->ParseAndExecute("struct Apple : Fruit{};")) {
+ consumeError(std::move(R));
+ return;
+ }
+ if (auto R = Interp->ParseAndExecute("void takeFruit(Fruit &f){}")) {
+ consumeError(std::move(R));
+ return;
+ }
+ if (auto R = Interp->ParseAndExecute("Apple a1;")) {
+ consumeError(std::move(R));
+ return;
+ }
+ if (auto R = Interp->ParseAndExecute("Fruit f1;")) {
+ consumeError(std::move(R));
+ return;
+ }
+ auto Err = llvm::Error::success();
+ auto comps = runComp(*Interp, std::string("takeFruit("), Err);
+ EXPECT_EQ((size_t)2, comps.size());
+ EXPECT_EQ(comps[0], std::string("a1"));
+ EXPECT_EQ(comps[1], std::string("f1"));
+ EXPECT_EQ((bool)Err, false);
+}
+
+TEST(CodeCompletionTest, MultipleArguments) {
+ auto Interp = createInterpreter();
+ if (auto R = Interp->ParseAndExecute("int foo = 42;")) {
+ consumeError(std::move(R));
+ return;
+ }
+ if (auto R = Interp->ParseAndExecute("char fowl = 'A';")) {
+ consumeError(std::move(R));
+ return;
+ }
+ if (auto R = Interp->ParseAndExecute("void takeTwo(int &a, char b){}")) {
+ consumeError(std::move(R));
+ return;
+ }
+ auto Err = llvm::Error::success();
+ auto comps = runComp(*Interp, std::string("takeTwo(foo, "), Err);
+ EXPECT_EQ((size_t)1, comps.size());
+ EXPECT_EQ(comps[0], std::string("fowl"));
+ EXPECT_EQ((bool)Err, false);
+}
+
+TEST(CodeCompletionTest, Methods) {
+ auto Interp = createInterpreter();
+ cantFail(Interp->ParseAndExecute(
+ "struct Foo{int add(int a){return 42;} int par(int b){return 42;}};"));
+ cantFail(Interp->ParseAndExecute("Foo f1;"));
+
+ auto Err = llvm::Error::success();
+ auto comps = runComp(*Interp, std::string("f1."), Err);
+ EXPECT_EQ((size_t)2, comps.size());
+ EXPECT_EQ(comps[0], std::string("add"));
+ EXPECT_EQ(comps[1], std::string("par"));
+ EXPECT_EQ((bool)Err, false);
+}
+
+TEST(CodeCompletionTest, MethodsInvocations) {
+ auto Interp = createInterpreter();
+ cantFail(Interp->ParseAndExecute(
+ "struct Foo{int add(int a){return 42;} int par(int b){return 42;}};"));
+ cantFail(Interp->ParseAndExecute("Foo f1;"));
+ cantFail(Interp->ParseAndExecute("int a = 84;"));
+
+ auto Err = llvm::Error::success();
+ auto comps = runComp(*Interp, std::string("f1.add("), Err);
+ EXPECT_EQ((size_t)1, comps.size());
+ EXPECT_EQ(comps[0], std::string("a"));
+ EXPECT_EQ((bool)Err, false);
+}
+
+TEST(CodeCompletionTest, NestedInvocations) {
+ auto Interp = createInterpreter();
+ cantFail(Interp->ParseAndExecute(
+ "struct Foo{int add(int a){return 42;} int par(int b){return 42;}};"));
+ cantFail(Interp->ParseAndExecute("Foo f1;"));
+ cantFail(Interp->ParseAndExecute("int a = 84;"));
+ cantFail(Interp->ParseAndExecute("int plus(int a, int b) { return a + b; }"));
+
+ auto Err = llvm::Error::success();
+ auto comps = runComp(*Interp, std::string("plus(42, f1.add("), Err);
+ EXPECT_EQ((size_t)1, comps.size());
+ EXPECT_EQ(comps[0], std::string("a"));
+ EXPECT_EQ((bool)Err, false);
+}
+
+TEST(CodeCompletionTest, TemplateFunctions) {
+ auto Interp = createInterpreter();
+ cantFail(
+ Interp->ParseAndExecute("template <typename T> T id(T a) { return a;} "));
+ cantFail(Interp->ParseAndExecute("int apple = 84;"));
+ {
+ auto Err = llvm::Error::success();
+ auto comps = runComp(*Interp, std::string("id<int>("), Err);
+ EXPECT_EQ((size_t)1, comps.size());
+ EXPECT_EQ(comps[0], std::string("apple"));
+ EXPECT_EQ((bool)Err, false);
+ }
+
+ cantFail(Interp->ParseAndExecute(
+ "template <typename T> T pickFirst(T a, T b) { return a;} "));
+ cantFail(Interp->ParseAndExecute("char pear = '4';"));
+ {
+ auto Err = llvm::Error::success();
+ auto comps = runComp(*Interp, std::string("pickFirst(apple, "), Err);
+ EXPECT_EQ((size_t)1, comps.size());
+ EXPECT_EQ(comps[0], std::string("apple"));
+ EXPECT_EQ((bool)Err, false);
+ }
+}
+
} // anonymous namespace
>From 4d27f1be36b0fd4cc8a6b6503bd049ad0514a583 Mon Sep 17 00:00:00 2001
From: Fred Fu <moonsolo at gmail.com>
Date: Mon, 6 Nov 2023 12:07:13 -0500
Subject: [PATCH 2/9] Update clang/lib/Interpreter/CodeCompletion.cpp
Co-authored-by: Vassil Vassilev <v.g.vassilev at gmail.com>
---
clang/lib/Interpreter/CodeCompletion.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/Interpreter/CodeCompletion.cpp b/clang/lib/Interpreter/CodeCompletion.cpp
index 0eb493d1b894b39..a53495ab42a345a 100644
--- a/clang/lib/Interpreter/CodeCompletion.cpp
+++ b/clang/lib/Interpreter/CodeCompletion.cpp
@@ -72,7 +72,7 @@ class ReplCompletionConsumer : public CodeCompleteConsumer {
ReplCodeCompletion &CC;
};
-class CompletionContextHanndler {
+class CompletionContextHandler {
protected:
CodeCompletionContext CCC;
std::vector<std::string> &Results;
>From 0fb2ead08680c3eeaa830799a52c64e525fdd049 Mon Sep 17 00:00:00 2001
From: Fred Fu <moonsolo at gmail.com>
Date: Mon, 6 Nov 2023 12:07:47 -0500
Subject: [PATCH 3/9] Update clang/lib/Interpreter/CodeCompletion.cpp
Co-authored-by: Vassil Vassilev <v.g.vassilev at gmail.com>
---
clang/lib/Interpreter/CodeCompletion.cpp | 17 +++++++++--------
1 file changed, 9 insertions(+), 8 deletions(-)
diff --git a/clang/lib/Interpreter/CodeCompletion.cpp b/clang/lib/Interpreter/CodeCompletion.cpp
index a53495ab42a345a..afb1025d0d06a44 100644
--- a/clang/lib/Interpreter/CodeCompletion.cpp
+++ b/clang/lib/Interpreter/CodeCompletion.cpp
@@ -93,16 +93,17 @@ class DotMemberAccessHandler : public CompletionContextHanndler {
std::vector<std::string> &Results)
: CompletionContextHanndler(CCC, Results) {}
void handleDeclaration(const CodeCompletionResult &Result) override {
- if (auto *ID = Result.Declaration->getIdentifier()) {
- if (const auto *Fun = llvm::dyn_cast<CXXMethodDecl>(Result.Declaration)) {
- if (Fun->getParent()->getCanonicalDecl() ==
- CCC.getBaseType()->getAsCXXRecordDecl()->getCanonicalDecl()) {
- LLVM_DEBUG(llvm::dbgs() << "[In HandleCodeCompleteDOT] Name : "
- << ID->getName() << "\n");
+ if (!Result.Declaration->getIdentifier())
+ return;
+ if (!isa<CXXMethodDecl>(Result.Declaration))
+ return;
+ const auto *Fun = cast<CXXMethodDecl>(Result.Declaration)
+ if (Fun->getParent()->getCanonicalDecl() ==
+ CCC.getBaseType()->getAsCXXRecordDecl()->getCanonicalDecl()) {
+ LLVM_DEBUG(llvm::dbgs() << "[In HandleCodeCompleteDOT] Name : "
+ << ID->getName() << "\n");
Results.push_back(ID->getName().str());
- }
}
- }
}
};
>From 2c44f9f6b4a9f77737548493cb7726dc776aed29 Mon Sep 17 00:00:00 2001
From: Fred Fu <moonsolo at gmail.com>
Date: Mon, 6 Nov 2023 12:07:54 -0500
Subject: [PATCH 4/9] Update clang/lib/Interpreter/CodeCompletion.cpp
Co-authored-by: Vassil Vassilev <v.g.vassilev at gmail.com>
---
clang/lib/Interpreter/CodeCompletion.cpp | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Interpreter/CodeCompletion.cpp b/clang/lib/Interpreter/CodeCompletion.cpp
index afb1025d0d06a44..6751e9215ab4a63 100644
--- a/clang/lib/Interpreter/CodeCompletion.cpp
+++ b/clang/lib/Interpreter/CodeCompletion.cpp
@@ -117,7 +117,9 @@ class DefaultAccessHandler : public CompletionContextHanndler {
: CompletionContextHanndler(CCC, Results), S(S) {}
void handleDeclaration(const CodeCompletionResult &Result) override {
auto PreferredType = CCC.getPreferredType();
- if (!PreferredType.isNull()) {
+ if (PreferredType.isNull())
+ return;
+ ...
if (auto *VD = dyn_cast<VarDecl>(Result.Declaration)) {
auto ArgumentType = VD->getType();
if (PreferredType->isReferenceType()) {
>From 294756add533c378caf317c2f480e217a817ecb3 Mon Sep 17 00:00:00 2001
From: Fred Fu <moonsolo at gmail.com>
Date: Mon, 6 Nov 2023 12:08:11 -0500
Subject: [PATCH 5/9] Update clang/lib/Interpreter/CodeCompletion.cpp
Co-authored-by: Vassil Vassilev <v.g.vassilev at gmail.com>
---
clang/lib/Interpreter/CodeCompletion.cpp | 1 -
1 file changed, 1 deletion(-)
diff --git a/clang/lib/Interpreter/CodeCompletion.cpp b/clang/lib/Interpreter/CodeCompletion.cpp
index 6751e9215ab4a63..e07dbf39e815945 100644
--- a/clang/lib/Interpreter/CodeCompletion.cpp
+++ b/clang/lib/Interpreter/CodeCompletion.cpp
@@ -226,7 +226,6 @@ class ExternalSource : public clang::ExternalASTSource {
ASTContext &ParentASTCtxt, FileManager &ParentFM);
bool FindExternalVisibleDeclsByName(const DeclContext *DC,
DeclarationName Name) override;
- // void CompleteType(TagDecl *Tag) override;
void
completeVisibleDeclsMap(const clang::DeclContext *childDeclContext) override;
};
>From 8ed0ccfee3c778643341b33429a9c11a4c1ce31d Mon Sep 17 00:00:00 2001
From: Fred Fu <moonsolo at gmail.com>
Date: Mon, 6 Nov 2023 12:25:41 -0500
Subject: [PATCH 6/9] wip
---
.../clang/Interpreter/CodeCompletion.h | 18 +++-
clang/lib/Interpreter/CodeCompletion.cpp | 82 +++++++++----------
clang/tools/clang-repl/ClangRepl.cpp | 2 +-
.../Interpreter/CodeCompletionTest.cpp | 2 +-
4 files changed, 59 insertions(+), 45 deletions(-)
diff --git a/clang/include/clang/Interpreter/CodeCompletion.h b/clang/include/clang/Interpreter/CodeCompletion.h
index 83f665b7a2db944..0c181b3e0920bad 100644
--- a/clang/include/clang/Interpreter/CodeCompletion.h
+++ b/clang/include/clang/Interpreter/CodeCompletion.h
@@ -23,9 +23,23 @@ namespace clang {
class CodeCompletionResult;
class CompilerInstance;
-struct ReplCodeCompletion {
- ReplCodeCompletion() = default;
+struct ReplCodeCompleter {
+ ReplCodeCompleter() = default;
std::string Prefix;
+
+ /*
+ @param InterpCI The compiler instance that is used to trigger code completion
+
+ @param Content The string where code completion is triggered.
+
+ @param Line The line number of the code completion point.
+
+ @param Col The column number of the code completion point.
+
+ @param ParentCI The running interpreter compiler instance that provides ASTContexts.
+
+ @param CCResults [out] The completion results.
+ */
void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content,
unsigned Line, unsigned Col,
const CompilerInstance *ParentCI,
diff --git a/clang/lib/Interpreter/CodeCompletion.cpp b/clang/lib/Interpreter/CodeCompletion.cpp
index e07dbf39e815945..10e9405f5d45bca 100644
--- a/clang/lib/Interpreter/CodeCompletion.cpp
+++ b/clang/lib/Interpreter/CodeCompletion.cpp
@@ -52,7 +52,7 @@ class CodeCompletionSubContext {
class ReplCompletionConsumer : public CodeCompleteConsumer {
public:
ReplCompletionConsumer(std::vector<std::string> &Results,
- ReplCodeCompletion &CC)
+ ReplCodeCompleter &CC)
: CodeCompleteConsumer(getClangCompleteOpts()),
CCAllocator(std::make_shared<GlobalCodeCompletionAllocator>()),
CCTUInfo(CCAllocator), Results(Results), CC(CC) {}
@@ -69,7 +69,7 @@ class ReplCompletionConsumer : public CodeCompleteConsumer {
std::shared_ptr<GlobalCodeCompletionAllocator> CCAllocator;
CodeCompletionTUInfo CCTUInfo;
std::vector<std::string> &Results;
- ReplCodeCompletion &CC;
+ ReplCodeCompleter &CC;
};
class CompletionContextHandler {
@@ -78,8 +78,8 @@ class CompletionContextHandler {
std::vector<std::string> &Results;
public:
- CompletionContextHanndler(CodeCompletionContext CCC,
- std::vector<std::string> &Results)
+ CompletionContextHandler(CodeCompletionContext CCC,
+ std::vector<std::string> &Results)
: CCC(CCC), Results(Results) {}
virtual void handleDeclaration(const CodeCompletionResult &Result) {}
virtual void handleKeyword(const CodeCompletionResult &Result) {}
@@ -87,62 +87,62 @@ class CompletionContextHandler {
virtual void handleMacro(const CodeCompletionResult &Result) {}
};
-class DotMemberAccessHandler : public CompletionContextHanndler {
+class DotMemberAccessHandler : public CompletionContextHandler {
public:
DotMemberAccessHandler(CodeCompletionContext CCC,
std::vector<std::string> &Results)
- : CompletionContextHanndler(CCC, Results) {}
+ : CompletionContextHandler(CCC, Results) {}
void handleDeclaration(const CodeCompletionResult &Result) override {
- if (!Result.Declaration->getIdentifier())
+ auto *ID = Result.Declaration->getIdentifier();
+ if (!ID)
return;
if (!isa<CXXMethodDecl>(Result.Declaration))
return;
- const auto *Fun = cast<CXXMethodDecl>(Result.Declaration)
+ const auto *Fun = cast<CXXMethodDecl>(Result.Declaration);
if (Fun->getParent()->getCanonicalDecl() ==
CCC.getBaseType()->getAsCXXRecordDecl()->getCanonicalDecl()) {
- LLVM_DEBUG(llvm::dbgs() << "[In HandleCodeCompleteDOT] Name : "
- << ID->getName() << "\n");
- Results.push_back(ID->getName().str());
- }
+ LLVM_DEBUG(llvm::dbgs() << "[In HandleCodeCompleteDOT] Name : "
+ << ID->getName() << "\n");
+ Results.push_back(ID->getName().str());
+ }
}
};
-class DefaultAccessHandler : public CompletionContextHanndler {
+class DefaultAccessHandler : public CompletionContextHandler {
private:
Sema &S;
public:
DefaultAccessHandler(Sema &S, CodeCompletionContext CCC,
std::vector<std::string> &Results)
- : CompletionContextHanndler(CCC, Results), S(S) {}
+ : CompletionContextHandler(CCC, Results), S(S) {}
void handleDeclaration(const CodeCompletionResult &Result) override {
auto PreferredType = CCC.getPreferredType();
if (PreferredType.isNull())
return;
- ...
- if (auto *VD = dyn_cast<VarDecl>(Result.Declaration)) {
- auto ArgumentType = VD->getType();
- if (PreferredType->isReferenceType()) {
- QualType RT =
- PreferredType->castAs<ReferenceType>()->getPointeeType();
- Sema::ReferenceConversions RefConv;
- Sema::ReferenceCompareResult RefRelationship =
- S.CompareReferenceRelationship(SourceLocation(), RT, ArgumentType,
- &RefConv);
- switch (RefRelationship) {
- case Sema::Ref_Compatible:
- case Sema::Ref_Related:
- Results.push_back(VD->getName().str());
- break;
- case Sema::Ref_Incompatible:
- break;
- }
- } else if (S.Context.hasSameType(ArgumentType, PreferredType)) {
+
+ if (auto *VD = dyn_cast<VarDecl>(Result.Declaration)) {
+ auto ArgumentType = VD->getType();
+ if (PreferredType->isReferenceType()) {
+ QualType RT = PreferredType->castAs<ReferenceType>()->getPointeeType();
+ Sema::ReferenceConversions RefConv;
+ Sema::ReferenceCompareResult RefRelationship =
+ S.CompareReferenceRelationship(SourceLocation(), RT, ArgumentType,
+ &RefConv);
+ switch (RefRelationship) {
+ case Sema::Ref_Compatible:
+ case Sema::Ref_Related:
Results.push_back(VD->getName().str());
+ break;
+ case Sema::Ref_Incompatible:
+ break;
}
+ } else if (S.Context.hasSameType(ArgumentType, PreferredType)) {
+ Results.push_back(VD->getName().str());
}
- } else
- Results.push_back(Result.Declaration->getName().str());
+ }
+
+ Results.push_back(Result.Declaration->getName().str());
}
void handleKeyword(const CodeCompletionResult &Result) override {
@@ -165,7 +165,7 @@ void ReplCompletionConsumer::ProcessCodeCompleteResults(
auto Prefix = S.getPreprocessor().getCodeCompletionFilter();
CC.Prefix = Prefix;
- std::unique_ptr<CompletionContextHanndler> CCH;
+ std::unique_ptr<CompletionContextHandler> CCH;
switch (Context.getKind()) {
case CodeCompletionContext::CCC_DotMemberAccess:
@@ -342,11 +342,11 @@ void ExternalSource::completeVisibleDeclsMap(
}
}
-void ReplCodeCompletion::codeComplete(CompilerInstance *InterpCI,
- llvm::StringRef Content, unsigned Line,
- unsigned Col,
- const CompilerInstance *ParentCI,
- std::vector<std::string> &CCResults) {
+void ReplCodeCompleter::codeComplete(CompilerInstance *InterpCI,
+ llvm::StringRef Content, unsigned Line,
+ unsigned Col,
+ const CompilerInstance *ParentCI,
+ std::vector<std::string> &CCResults) {
auto DiagOpts = DiagnosticOptions();
auto consumer = ReplCompletionConsumer(CCResults, *this);
diff --git a/clang/tools/clang-repl/ClangRepl.cpp b/clang/tools/clang-repl/ClangRepl.cpp
index fba4b88c9c7dc70..9d946f0163181a1 100644
--- a/clang/tools/clang-repl/ClangRepl.cpp
+++ b/clang/tools/clang-repl/ClangRepl.cpp
@@ -127,7 +127,7 @@ ReplListCompleter::operator()(llvm::StringRef Buffer, size_t Pos,
}
auto *MainCI =
const_cast<clang::CompilerInstance *>((*Interp)->getCompilerInstance());
- auto CC = clang::ReplCodeCompletion();
+ auto CC = clang::ReplCodeCompleter();
CC.codeComplete(MainCI, Buffer, Lines, Pos + 1,
MainInterp.getCompilerInstance(), Results);
for (auto c : Results) {
diff --git a/clang/unittests/Interpreter/CodeCompletionTest.cpp b/clang/unittests/Interpreter/CodeCompletionTest.cpp
index 918b4e8167eb73c..99213a5e4794b07 100644
--- a/clang/unittests/Interpreter/CodeCompletionTest.cpp
+++ b/clang/unittests/Interpreter/CodeCompletionTest.cpp
@@ -41,7 +41,7 @@ static std::vector<std::string> runComp(clang::Interpreter &MainInterp,
std::vector<std::string> Comps;
auto *MainCI =
const_cast<clang::CompilerInstance *>((*Interp)->getCompilerInstance());
- auto CC = ReplCodeCompletion();
+ auto CC = ReplCodeCompleter();
CC.codeComplete(MainCI, Input, /* Lines */ 1, Input.size() + 1,
MainInterp.getCompilerInstance(), Results);
>From 488a878adafa74c13bf96489aecf5d7b8a79a6fc Mon Sep 17 00:00:00 2001
From: Fred Fu <moonsolo at gmail.com>
Date: Mon, 6 Nov 2023 12:28:09 -0500
Subject: [PATCH 7/9] wip
---
clang/lib/Interpreter/CodeCompletion.cpp | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/clang/lib/Interpreter/CodeCompletion.cpp b/clang/lib/Interpreter/CodeCompletion.cpp
index 10e9405f5d45bca..493361177caa068 100644
--- a/clang/lib/Interpreter/CodeCompletion.cpp
+++ b/clang/lib/Interpreter/CodeCompletion.cpp
@@ -118,8 +118,10 @@ class DefaultAccessHandler : public CompletionContextHandler {
: CompletionContextHandler(CCC, Results), S(S) {}
void handleDeclaration(const CodeCompletionResult &Result) override {
auto PreferredType = CCC.getPreferredType();
- if (PreferredType.isNull())
+ if (PreferredType.isNull()) {
+ Results.push_back(Result.Declaration->getName().str());
return;
+ }
if (auto *VD = dyn_cast<VarDecl>(Result.Declaration)) {
auto ArgumentType = VD->getType();
@@ -141,8 +143,6 @@ class DefaultAccessHandler : public CompletionContextHandler {
Results.push_back(VD->getName().str());
}
}
-
- Results.push_back(Result.Declaration->getName().str());
}
void handleKeyword(const CodeCompletionResult &Result) override {
>From e1f6b9362963f174c0153ae01de30e59af6381d2 Mon Sep 17 00:00:00 2001
From: Fred Fu <moonsolo at gmail.com>
Date: Mon, 6 Nov 2023 13:22:32 -0500
Subject: [PATCH 8/9] wip
---
clang/lib/Interpreter/CodeCompletion.cpp | 17 ++++++++++++++++-
1 file changed, 16 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Interpreter/CodeCompletion.cpp b/clang/lib/Interpreter/CodeCompletion.cpp
index 493361177caa068..078feb4af4318ee 100644
--- a/clang/lib/Interpreter/CodeCompletion.cpp
+++ b/clang/lib/Interpreter/CodeCompletion.cpp
@@ -10,7 +10,6 @@
//
//===----------------------------------------------------------------------===//
-#include "clang/Interpreter/CodeCompletion.h"
#include "clang/AST/ASTImporter.h"
#include "clang/AST/DeclLookups.h"
#include "clang/AST/DeclarationName.h"
@@ -19,6 +18,7 @@
#include "clang/Frontend/ASTUnit.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendActions.h"
+#include "clang/Interpreter/CodeCompletion.h"
#include "clang/Interpreter/Interpreter.h"
#include "clang/Lex/PreprocessorOptions.h"
#include "clang/Sema/CodeCompleteConsumer.h"
@@ -72,6 +72,13 @@ class ReplCompletionConsumer : public CodeCompleteConsumer {
ReplCodeCompleter &CC;
};
+/*
+ The abstract class CompletionContextHandler contains four interfaces, each of
+ which handles one type of completion result.
+
+ Its substract classes are used to create concrete handlers based on
+ CodeCompletionContext.
+ */
class CompletionContextHandler {
protected:
CodeCompletionContext CCC;
@@ -81,9 +88,17 @@ class CompletionContextHandler {
CompletionContextHandler(CodeCompletionContext CCC,
std::vector<std::string> &Results)
: CCC(CCC), Results(Results) {}
+
+ // convert a Declaration completion result to a completion string, and then store it in Results.
virtual void handleDeclaration(const CodeCompletionResult &Result) {}
+
+ // convert a Keyword completion result to a completion string, and then store it in Results.
virtual void handleKeyword(const CodeCompletionResult &Result) {}
+
+ // convert a Pattern completion result to a completion string, and then store it in Results.
virtual void handlePattern(const CodeCompletionResult &Result) {}
+
+ // convert a Macro completion result to a completion string, and then store it in Results.
virtual void handleMacro(const CodeCompletionResult &Result) {}
};
>From a583adaa667e71cde3d09f50875733a8312e8335 Mon Sep 17 00:00:00 2001
From: Fred Fu <moonsolo at gmail.com>
Date: Mon, 6 Nov 2023 13:40:23 -0500
Subject: [PATCH 9/9] wip
---
clang/lib/Interpreter/CodeCompletion.cpp | 84 ++++++++++++------------
1 file changed, 43 insertions(+), 41 deletions(-)
diff --git a/clang/lib/Interpreter/CodeCompletion.cpp b/clang/lib/Interpreter/CodeCompletion.cpp
index 078feb4af4318ee..c2e7383600b07d1 100644
--- a/clang/lib/Interpreter/CodeCompletion.cpp
+++ b/clang/lib/Interpreter/CodeCompletion.cpp
@@ -310,48 +310,50 @@ void ExternalSource::completeVisibleDeclsMap(
for (auto *DeclCtxt = ParentTUDeclCtxt; DeclCtxt != nullptr;
DeclCtxt = DeclCtxt->getPreviousDecl()) {
for (auto &IDeclContext : DeclCtxt->decls()) {
- if (NamedDecl *Decl = llvm::dyn_cast<NamedDecl>(IDeclContext)) {
- if (auto DeclOrErr = Importer->Import(Decl)) {
- if (NamedDecl *importedNamedDecl =
- llvm::dyn_cast<NamedDecl>(*DeclOrErr)) {
- SetExternalVisibleDeclsForName(ChildDeclContext,
- importedNamedDecl->getDeclName(),
- importedNamedDecl);
- if (auto *Record =
- llvm::dyn_cast<CXXRecordDecl>(importedNamedDecl)) {
- if (auto Err = Importer->ImportDefinition(Decl))
- consumeError(std::move(Err));
- // LLVM_DEBUG(llvm::dbgs() << "\nHello :\n");
- // Record->getTypeForDecl()->dump();
- Record->setHasLoadedFieldsFromExternalStorage(true);
- // auto ToOrErr = Importer->Import(->getTypeForDecl());
- // if (!ToOrErr) {
- // consumeError(std::move(ToOrErr.takeError()));
- // }
- LLVM_DEBUG(
- llvm::dbgs()
- << "\nCXXRecrod : " << Record->getName() << " size(methods): "
- << std::distance(Record->method_begin(), Record->method_end())
- << " has def?: " << Record->hasDefinition()
- << " # (methods): "
- << std::distance(Record->getDefinition()->method_begin(),
- Record->getDefinition()->method_end())
- << "\n");
- for (auto *Meth : Record->methods()) {
- SetExternalVisibleDeclsForName(ChildDeclContext,
- Meth->getDeclName(), Meth);
- // if (Meth->getDeclName().isIdentifier()) {
- // LLVM_DEBUG(llvm::dbgs() << "CXXRecrod Method: " <<
- // Meth->getName() << "\n");
-
- // }
- }
- }
- }
- } else {
- llvm::consumeError(DeclOrErr.takeError());
- }
+ if (!llvm::isa<NamedDecl>(IDeclContext))
+ continue;
+
+ NamedDecl *Decl = llvm::cast<NamedDecl>(IDeclContext);
+
+ auto DeclOrErr = Importer->Import(Decl);
+ if (!DeclOrErr) {
+ llvm::consumeError(DeclOrErr.takeError());
+ continue;
}
+
+ if (!llvm::isa<NamedDecl>(*DeclOrErr))
+ continue;
+
+ NamedDecl *importedNamedDecl = llvm::cast<NamedDecl>(*DeclOrErr);
+
+ SetExternalVisibleDeclsForName(ChildDeclContext,
+ importedNamedDecl->getDeclName(),
+ importedNamedDecl);
+
+ if (!llvm::isa<CXXRecordDecl>(importedNamedDecl))
+ continue;
+
+ auto *Record =
+ llvm::cast<CXXRecordDecl>(importedNamedDecl);
+
+ if (auto Err = Importer->ImportDefinition(Decl)) {
+ consumeError(std::move(Err));
+ continue;
+ }
+
+ Record->setHasLoadedFieldsFromExternalStorage(true);
+ LLVM_DEBUG(
+ llvm::dbgs()
+ << "\nCXXRecrod : " << Record->getName() << " size(methods): "
+ << std::distance(Record->method_begin(), Record->method_end())
+ << " has def?: " << Record->hasDefinition()
+ << " # (methods): "
+ << std::distance(Record->getDefinition()->method_begin(),
+ Record->getDefinition()->method_end())
+ << "\n");
+ for (auto *Meth : Record->methods())
+ SetExternalVisibleDeclsForName(ChildDeclContext,
+ Meth->getDeclName(), Meth);
}
ChildDeclContext->setHasExternalLexicalStorage(false);
}
More information about the cfe-commits
mailing list