summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabor Marton <gabor.marton@ericsson.com>2019-07-17 13:47:46 +0000
committerGabor Marton <gabor.marton@ericsson.com>2019-07-17 13:47:46 +0000
commitd98a292c08cfe54f5bf7a7ef11af8a78d9bdc28c (patch)
tree442f96fc7efdea9064a043bc806e88c0673d959c
parent1abfc06ea7afe446de65589c3e2f2c6cf4a699ba (diff)
[ASTImporter] Fix LLDB lookup in transparent ctx and with ext src
Summary: With LLDB we use localUncachedLookup(), however, that fails to find Decls when a transparent context is involved and the given DC has external lexical storage. The solution is to use noload_lookup, which works well with transparent contexts. But, we cannot use only the noload_lookup since the slow case of localUncachedLookup is still needed in some other cases. These other cases are handled in ASTImporterLookupTable, but we cannot use that with LLDB since that traverses through the AST which initiates the load of external decls again via DC::decls(). We must avoid loading external decls during the import becuase ExternalASTSource is implemented with ASTImporter, so external loads during import results in uncontrolled and faulty import. Reviewers: shafik, teemperor, jingham, clayborg, a_sidorin, a.sidorin Subscribers: rnkovacs, dkrupp, Szelethus, gamesh411, cfe-commits, lldb-commits Tags: #clang, #lldb Differential Revision: https://reviews.llvm.org/D61333 git-svn-id: https://llvm.org/svn/llvm-project/lldb/trunk@366325 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--packages/Python/lldbsuite/test/lang/c/modules/TestCModules.py14
-rw-r--r--packages/Python/lldbsuite/test/lang/c/modules/main.c4
-rw-r--r--source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp30
3 files changed, 34 insertions, 14 deletions
diff --git a/packages/Python/lldbsuite/test/lang/c/modules/TestCModules.py b/packages/Python/lldbsuite/test/lang/c/modules/TestCModules.py
index 455704280..857223b5e 100644
--- a/packages/Python/lldbsuite/test/lang/c/modules/TestCModules.py
+++ b/packages/Python/lldbsuite/test/lang/c/modules/TestCModules.py
@@ -47,6 +47,10 @@ class CModulesTestCase(TestBase):
self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
substrs=[' resolved, hit count = 1'])
+ # Enable logging of the imported AST.
+ log_file = os.path.join(self.getBuildDir(), "lldb-ast-log.txt")
+ self.runCmd("log enable lldb ast -f '%s'" % log_file)
+
self.expect(
"expr -l objc++ -- @import Darwin; 3",
VARIABLES_DISPLAYED_CORRECTLY,
@@ -54,6 +58,8 @@ class CModulesTestCase(TestBase):
"int",
"3"])
+ # This expr command imports __sFILE with definition
+ # (FILE is a typedef to __sFILE.)
self.expect(
"expr *fopen(\"/dev/zero\", \"w\")",
VARIABLES_DISPLAYED_CORRECTLY,
@@ -61,6 +67,14 @@ class CModulesTestCase(TestBase):
"FILE",
"_close"])
+ # Check that the AST log contains exactly one definition of __sFILE.
+ f = open(log_file)
+ log_lines = f.readlines()
+ f.close()
+ os.remove(log_file)
+ self.assertEqual(" ".join(log_lines).count("struct __sFILE definition"),
+ 1)
+
self.expect("expr *myFile", VARIABLES_DISPLAYED_CORRECTLY,
substrs=["a", "5", "b", "9"])
diff --git a/packages/Python/lldbsuite/test/lang/c/modules/main.c b/packages/Python/lldbsuite/test/lang/c/modules/main.c
index 2b244bc38..df321a75f 100644
--- a/packages/Python/lldbsuite/test/lang/c/modules/main.c
+++ b/packages/Python/lldbsuite/test/lang/c/modules/main.c
@@ -5,11 +5,11 @@ int printf(const char * __restrict format, ...);
typedef struct {
int a;
int b;
-} FILE;
+} MYFILE;
int main()
{
- FILE *myFile = malloc(sizeof(FILE));
+ MYFILE *myFile = malloc(sizeof(MYFILE));
myFile->a = 5;
myFile->b = 9;
diff --git a/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp b/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp
index 8d29df9dd..c5778f86b 100644
--- a/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp
+++ b/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp
@@ -612,10 +612,15 @@ void ClangASTSource::FindExternalLexicalDecls(
if (!original_decl_context)
return;
+ // Indicates whether we skipped any Decls of the original DeclContext.
+ bool SkippedDecls = false;
for (TagDecl::decl_iterator iter = original_decl_context->decls_begin();
iter != original_decl_context->decls_end(); ++iter) {
Decl *decl = *iter;
+ // The predicate function returns true if the passed declaration kind is
+ // the one we are looking for.
+ // See clang::ExternalASTSource::FindExternalLexicalDecls()
if (predicate(decl->getKind())) {
if (log) {
ASTDumper ast_dumper(decl);
@@ -640,21 +645,22 @@ void ClangASTSource::FindExternalLexicalDecls(
m_ast_importer_sp->RequireCompleteType(copied_field_type);
}
-
- DeclContext *decl_context_non_const =
- const_cast<DeclContext *>(decl_context);
-
- if (copied_decl->getDeclContext() != decl_context) {
- if (copied_decl->getDeclContext()->containsDecl(copied_decl))
- copied_decl->getDeclContext()->removeDecl(copied_decl);
- copied_decl->setDeclContext(decl_context_non_const);
- }
-
- if (!decl_context_non_const->containsDecl(copied_decl))
- decl_context_non_const->addDeclInternal(copied_decl);
+ } else {
+ SkippedDecls = true;
}
}
+ // CopyDecl may build a lookup table which may set up ExternalLexicalStorage
+ // to false. However, since we skipped some of the external Decls we must
+ // set it back!
+ if (SkippedDecls) {
+ decl_context->setHasExternalLexicalStorage(true);
+ // This sets HasLazyExternalLexicalLookups to true. By setting this bit we
+ // ensure that the lookup table is rebuilt, which means the external source
+ // is consulted again when a clang::DeclContext::lookup is called.
+ const_cast<DeclContext *>(decl_context)->setMustBuildLookupTable();
+ }
+
return;
}