From: Daniel Dunbar Date: Thu, 5 Nov 2009 02:42:12 +0000 (+0000) Subject: Replace DiagnosticClient::setLangOptions with {Begin,End}SourceFile, and clarify X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=efcbe9475348ecab6b85153baa21d0e894e39607;p=clang Replace DiagnosticClient::setLangOptions with {Begin,End}SourceFile, and clarify invariants (diagnostics with source informations must occur between {Begin,End}SourceFile). git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@86113 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/Diagnostic.h b/include/clang/Basic/Diagnostic.h index 005aab3fa6..77a2079b76 100644 --- a/include/clang/Basic/Diagnostic.h +++ b/include/clang/Basic/Diagnostic.h @@ -769,17 +769,28 @@ class DiagnosticClient { public: virtual ~DiagnosticClient(); - /// setLangOptions - This is set by clients of diagnostics when they know the - /// language parameters of the diagnostics that may be sent through. Note - /// that this can change over time if a DiagClient has multiple languages sent - /// through it. It may also be set to null (e.g. when processing command line - /// options). - virtual void setLangOptions(const LangOptions *LO) {} + /// BeginSourceFile - Callback to inform the diagnostic client that processing + /// of a source file is beginning. + /// + /// Note that diagnostics may be emitted outside the processing of a source + /// file, for example during the parsing of command line options. However, + /// diagnostics with source range information are required to only be emitted + /// in between BeginSourceFile() and EndSourceFile(). + /// + /// \arg LO - The language options for the source file being processed. + /// \arg PP - The preprocessor object being used for the source; this optional + /// and may not be present, for example when processing AST source files. + virtual void BeginSourceFile(const LangOptions &LangOpts) {} + + /// EndSourceFile - Callback to inform the diagnostic client that processing + /// of a source file has ended. The diagnostic client should assume that any + /// objects made available via \see BeginSourceFile() are inaccessible. + virtual void EndSourceFile() {} /// IncludeInDiagnosticCounts - This method (whose default implementation - /// returns true) indicates whether the diagnostics handled by this - /// DiagnosticClient should be included in the number of diagnostics - /// reported by Diagnostic. + /// returns true) indicates whether the diagnostics handled by this + /// DiagnosticClient should be included in the number of diagnostics reported + /// by Diagnostic. virtual bool IncludeInDiagnosticCounts() const; /// HandleDiagnostic - Handle this diagnostic, reporting it to the user or diff --git a/include/clang/Frontend/TextDiagnosticPrinter.h b/include/clang/Frontend/TextDiagnosticPrinter.h index 98e5a75c65..3c5c6267c6 100644 --- a/include/clang/Frontend/TextDiagnosticPrinter.h +++ b/include/clang/Frontend/TextDiagnosticPrinter.h @@ -39,8 +39,12 @@ class TextDiagnosticPrinter : public DiagnosticClient { public: TextDiagnosticPrinter(llvm::raw_ostream &os, const DiagnosticOptions &diags); - void setLangOptions(const LangOptions *LO) { - LangOpts = LO; + void BeginSourceFile(const LangOptions &LO) { + LangOpts = &LO; + } + + void EndSourceFile() { + LangOpts = 0; } void PrintIncludeStack(SourceLocation Loc, const SourceManager &SM); diff --git a/lib/Frontend/TextDiagnosticPrinter.cpp b/lib/Frontend/TextDiagnosticPrinter.cpp index b1d8800369..4f8c804844 100644 --- a/lib/Frontend/TextDiagnosticPrinter.cpp +++ b/lib/Frontend/TextDiagnosticPrinter.cpp @@ -268,6 +268,7 @@ void TextDiagnosticPrinter::EmitCaretDiagnostic(SourceLocation Loc, const CodeModificationHint *Hints, unsigned NumHints, unsigned Columns) { + assert(LangOpts && "Unexpected diagnostic outside source file processing"); assert(!Loc.isInvalid() && "must have a valid source location here"); // If this is a macro ID, first emit information about where this was diff --git a/tools/clang-cc/clang-cc.cpp b/tools/clang-cc/clang-cc.cpp index 06ae4edf0e..26ba42d670 100644 --- a/tools/clang-cc/clang-cc.cpp +++ b/tools/clang-cc/clang-cc.cpp @@ -1545,9 +1545,14 @@ public: Chain2.reset(new TextDiagnosticPrinter(*BuildLogFile, DiagOpts)); } - virtual void setLangOptions(const LangOptions *LO) { - Chain1->setLangOptions(LO); - Chain2->setLangOptions(LO); + virtual void BeginSourceFile(const LangOptions &LO) { + Chain1->BeginSourceFile(LO); + Chain2->BeginSourceFile(LO); + } + + virtual void EndSourceFile() { + Chain1->EndSourceFile(); + Chain2->EndSourceFile(); } virtual bool IncludeInDiagnosticCounts() const { @@ -2080,7 +2085,9 @@ static void ProcessASTInputFile(const std::string &InFile, ProgActions PA, AST->getSourceManager().createMainFileIDForMemBuffer(SB); // Stream the input AST to the consumer. + Diags.getClient()->BeginSourceFile(PP.getLangOptions()); ParseAST(PP, Consumer.get(), AST->getASTContext(), Stats); + Diags.getClient()->EndSourceFile(); // Release the consumer and the AST, in that order since the consumer may // perform actions in its destructor which require the context. @@ -2230,7 +2237,6 @@ int main(int argc, char **argv) { // Initialize language options, inferring file types from input filenames. LangOptions LangInfo; - DiagClient->setLangOptions(&LangInfo); InitializeLangOptions(LangInfo, LK); InitializeLanguageStandard(LangInfo, LK, Target.get(), Features); @@ -2276,10 +2282,11 @@ int main(int argc, char **argv) { } // Process the source file. + DiagClient->BeginSourceFile(LangInfo); ProcessInputFile(*PP, InFile, ProgAction, Features, Context); + DiagClient->EndSourceFile(); HeaderInfo.ClearFileInfo(); - DiagClient->setLangOptions(0); } if (!NoCaretDiagnostics)