From: Ben Langmuir Date: Tue, 22 Apr 2014 17:40:12 +0000 (+0000) Subject: Attempt to fix null ASTContext in ASTUnit error path X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=13e41baf7883704f60e86c06d794652330c2bd93;p=clang Attempt to fix null ASTContext in ASTUnit error path We don't need the ASTContext for the diagnostics, only the language options, which we can get from the compiler invocation. It worries me how many categorically different states the ASTUnit class can be in depending on how it is being constructed/used. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@206909 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Frontend/ASTUnit.h b/include/clang/Frontend/ASTUnit.h index f8cb14e29d..ceabaa75e9 100644 --- a/include/clang/Frontend/ASTUnit.h +++ b/include/clang/Frontend/ASTUnit.h @@ -501,6 +501,11 @@ public: assert(TheSema && "ASTUnit does not have a Sema object!"); return *TheSema; } + + const LangOptions &getLangOpts() const { + assert(LangOpts && " ASTUnit does not have language options"); + return *LangOpts; + } const FileManager &getFileManager() const { return *FileMgr; } FileManager &getFileManager() { return *FileMgr; } diff --git a/lib/Frontend/ASTUnit.cpp b/lib/Frontend/ASTUnit.cpp index 240acc2f50..b35fcf196d 100644 --- a/lib/Frontend/ASTUnit.cpp +++ b/lib/Frontend/ASTUnit.cpp @@ -1714,7 +1714,10 @@ void ASTUnit::RealizeTopLevelDeclsFromPreamble() { } void ASTUnit::transferASTDataFromCompilerInstance(CompilerInstance &CI) { - // Steal the created target, context, and preprocessor. + // Steal the created target, context, and preprocessor if they have been + // created. + assert(CI.hasInvocation() && "missing invocation"); + LangOpts = CI.getInvocation().getLangOpts(); TheSema.reset(CI.takeSema()); Consumer.reset(CI.takeASTConsumer()); if (CI.hasASTContext()) diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp index 1290c72067..9d59fa7c7b 100644 --- a/tools/libclang/CIndex.cpp +++ b/tools/libclang/CIndex.cpp @@ -6720,7 +6720,7 @@ void cxindex::printDiagsToStderr(ASTUnit *Unit) { for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(), DEnd = Unit->stored_diag_end(); D != DEnd; ++D) { - CXStoredDiagnostic Diag(*D, Unit->getASTContext().getLangOpts()); + CXStoredDiagnostic Diag(*D, Unit->getLangOpts()); CXString Msg = clang_formatDiagnostic(&Diag, clang_defaultDiagnosticDisplayOptions()); fprintf(stderr, "%s\n", clang_getCString(Msg));