Ignore generated @import statements in the expression evaluator

Summary:
The ClangModulesDeclVendor is currently interpreting all injected `@import` statements in our expression
wrapper as modules that the user has explicitly requested to be persistently loaded. As we inject
`@import` statements with our std module prototype, the ClangModulesDeclVendor will start compiling
and loading unrelated C++ modules because it thinks the user has requested that it should load them. As
the ClangModulesDeclVendor is lacking the setup to compile these modules (e.g. it lacks the include paths),
it will then actually just fail to compile them and cause the whole expression evaluation to fail. This causes
these tests to fail on systems that enable the ClangModulesDeclVendor (such as macOS).

This patch fixes this by preventing the ClangModulesDeclVendor from interpreting `@import` statements
in the wrapper source code. This is done by check if the import happens in the fake source file containing
our wrapper code (which implies it was generated by LLDB).

This patch doesn't reenable the tests as there is more work needed to get the tests running on macOS (D67760)

Reviewers: aprantl, shafik, jingham

Subscribers: lldb-commits

Tags: #c_modules_in_lldb, #lldb

Differential Revision: https://reviews.llvm.org/D61565

llvm-svn: 372690
This commit is contained in:
Raphael Isemann 2019-09-24 06:58:39 +00:00
parent bce849e350
commit bd2a910a63
3 changed files with 22 additions and 6 deletions

View File

@ -105,16 +105,26 @@ using namespace lldb_private;
class ClangExpressionParser::LLDBPreprocessorCallbacks : public PPCallbacks { class ClangExpressionParser::LLDBPreprocessorCallbacks : public PPCallbacks {
ClangModulesDeclVendor &m_decl_vendor; ClangModulesDeclVendor &m_decl_vendor;
ClangPersistentVariables &m_persistent_vars; ClangPersistentVariables &m_persistent_vars;
clang::SourceManager &m_source_mgr;
StreamString m_error_stream; StreamString m_error_stream;
bool m_has_errors = false; bool m_has_errors = false;
public: public:
LLDBPreprocessorCallbacks(ClangModulesDeclVendor &decl_vendor, LLDBPreprocessorCallbacks(ClangModulesDeclVendor &decl_vendor,
ClangPersistentVariables &persistent_vars) ClangPersistentVariables &persistent_vars,
: m_decl_vendor(decl_vendor), m_persistent_vars(persistent_vars) {} clang::SourceManager &source_mgr)
: m_decl_vendor(decl_vendor), m_persistent_vars(persistent_vars),
m_source_mgr(source_mgr) {}
void moduleImport(SourceLocation import_location, clang::ModuleIdPath path, void moduleImport(SourceLocation import_location, clang::ModuleIdPath path,
const clang::Module * /*null*/) override { const clang::Module * /*null*/) override {
// Ignore modules that are imported in the wrapper code as these are not
// loaded by the user.
llvm::StringRef filename =
m_source_mgr.getPresumedLoc(import_location).getFilename();
if (filename == ClangExpressionSourceCode::g_prefix_file_name)
return;
SourceModule module; SourceModule module;
for (const std::pair<IdentifierInfo *, SourceLocation> &component : path) for (const std::pair<IdentifierInfo *, SourceLocation> &component : path)
@ -572,8 +582,8 @@ ClangExpressionParser::ClangExpressionParser(ExecutionContextScope *exe_scope, E
llvm::cast<ClangPersistentVariables>( llvm::cast<ClangPersistentVariables>(
target_sp->GetPersistentExpressionStateForLanguage( target_sp->GetPersistentExpressionStateForLanguage(
lldb::eLanguageTypeC)); lldb::eLanguageTypeC));
std::unique_ptr<PPCallbacks> pp_callbacks( std::unique_ptr<PPCallbacks> pp_callbacks(new LLDBPreprocessorCallbacks(
new LLDBPreprocessorCallbacks(*decl_vendor, *clang_persistent_vars)); *decl_vendor, *clang_persistent_vars, m_compiler->getSourceManager()));
m_pp_callbacks = m_pp_callbacks =
static_cast<LLDBPreprocessorCallbacks *>(pp_callbacks.get()); static_cast<LLDBPreprocessorCallbacks *>(pp_callbacks.get());
m_compiler->getPreprocessor().addPPCallbacks(std::move(pp_callbacks)); m_compiler->getPreprocessor().addPPCallbacks(std::move(pp_callbacks));

View File

@ -29,9 +29,12 @@
using namespace lldb_private; using namespace lldb_private;
#define PREFIX_NAME "<lldb wrapper prefix>"
const llvm::StringRef ClangExpressionSourceCode::g_prefix_file_name = PREFIX_NAME;
const char *ClangExpressionSourceCode::g_expression_prefix = const char *ClangExpressionSourceCode::g_expression_prefix =
R"( "#line 1 \"" PREFIX_NAME R"("
#line 1 "<lldb wrapper prefix>"
#ifndef offsetof #ifndef offsetof
#define offsetof(t, d) __builtin_offsetof(t, d) #define offsetof(t, d) __builtin_offsetof(t, d)
#endif #endif

View File

@ -23,6 +23,9 @@ class ExecutionContext;
class ClangExpressionSourceCode : public ExpressionSourceCode { class ClangExpressionSourceCode : public ExpressionSourceCode {
public: public:
/// The file name we use for the wrapper code that we inject before
/// the user expression.
static const llvm::StringRef g_prefix_file_name;
static const char *g_expression_prefix; static const char *g_expression_prefix;
static ClangExpressionSourceCode *CreateWrapped(llvm::StringRef filename, static ClangExpressionSourceCode *CreateWrapped(llvm::StringRef filename,