From 2eefd8657c233bc7c9330acfe475fc270bbe7cab Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Tue, 11 Dec 2007 22:57:35 +0000 Subject: [PATCH] Modified the internals of Diagnostic and DiagnosticClient to use SourceManager*'s instead of SourceManager&'s. This allows the client specify a NULL SourceManager when using a default constructed SourceLocation. Thus the SourceManager can be NULL when the SourceLocation's isValid() == false. The interface to most clients of Diagnostic remains the same. Diagnostic::Report() is overload to either accept a SourceLocation and a SourceManager&, or neither. Thus clients that do not have a SourceManager cannot specify a SourceLocation. Modified TextDiagnostics* to use this new interface. Modified the driver to not passed in SourceManager when warning about "-I-". git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@44887 91177308-0d34-0410-b5e6-96231b3b80d8 --- Basic/Diagnostic.cpp | 2 +- Driver/TextDiagnosticBuffer.cpp | 2 +- Driver/TextDiagnosticBuffer.h | 2 +- Driver/TextDiagnosticPrinter.cpp | 32 ++++++++++++++++---------------- Driver/TextDiagnosticPrinter.h | 4 ++-- Driver/TextDiagnostics.cpp | 4 ++-- Driver/TextDiagnostics.h | 4 ++-- Driver/clang.cpp | 4 +--- include/clang/Basic/Diagnostic.h | 24 ++++++++++++++++++++---- 9 files changed, 46 insertions(+), 32 deletions(-) diff --git a/Basic/Diagnostic.cpp b/Basic/Diagnostic.cpp index 399419b85e..36da1e2c5c 100644 --- a/Basic/Diagnostic.cpp +++ b/Basic/Diagnostic.cpp @@ -198,7 +198,7 @@ Diagnostic::Level Diagnostic::getDiagnosticLevel(unsigned DiagID) const { /// compilation, return true, otherwise return false. DiagID is a member of /// the diag::kind enum. void Diagnostic::Report(SourceLocation Pos, unsigned DiagID, - SourceManager& SrcMgr, + SourceManager* SrcMgr, const std::string *Strs, unsigned NumStrs, const SourceRange *Ranges, unsigned NumRanges) { // Figure out the diagnostic level of this message. diff --git a/Driver/TextDiagnosticBuffer.cpp b/Driver/TextDiagnosticBuffer.cpp index c77801b767..f644c3f660 100644 --- a/Driver/TextDiagnosticBuffer.cpp +++ b/Driver/TextDiagnosticBuffer.cpp @@ -21,7 +21,7 @@ void TextDiagnosticBuffer::HandleDiagnostic(Diagnostic &Diags, Diagnostic::Level Level, SourceLocation Pos, diag::kind ID, - SourceManager& SrcMgr, + SourceManager* SrcMgr, const std::string *Strs, unsigned NumStrs, const SourceRange *, diff --git a/Driver/TextDiagnosticBuffer.h b/Driver/TextDiagnosticBuffer.h index 5da37c3e27..53a6cb7e60 100644 --- a/Driver/TextDiagnosticBuffer.h +++ b/Driver/TextDiagnosticBuffer.h @@ -41,7 +41,7 @@ public: virtual void HandleDiagnostic(Diagnostic &Diags, Diagnostic::Level DiagLevel, SourceLocation Pos, diag::kind ID, - SourceManager& SrcMgr, + SourceManager* SrcMgr, const std::string *Strs, unsigned NumStrs, const SourceRange *Ranges, diff --git a/Driver/TextDiagnosticPrinter.cpp b/Driver/TextDiagnosticPrinter.cpp index d2eaacaace..3df15158ae 100644 --- a/Driver/TextDiagnosticPrinter.cpp +++ b/Driver/TextDiagnosticPrinter.cpp @@ -47,24 +47,24 @@ PrintIncludeStack(SourceLocation Pos, SourceManager& SourceMgr) { /// HighlightRange - Given a SourceRange and a line number, highlight (with ~'s) /// any characters in LineNo that intersect the SourceRange. void TextDiagnosticPrinter::HighlightRange(const SourceRange &R, - SourceManager& SourceMgr, + SourceManager* SourceMgr, unsigned LineNo, std::string &CaratLine, - const std::string &SourceLine) { + const std::string &SourceLine) { assert(CaratLine.size() == SourceLine.size() && "Expect a correspondence between source and carat line!"); if (!R.isValid()) return; - unsigned StartLineNo = SourceMgr.getLogicalLineNumber(R.getBegin()); + unsigned StartLineNo = SourceMgr->getLogicalLineNumber(R.getBegin()); if (StartLineNo > LineNo) return; // No intersection. - unsigned EndLineNo = SourceMgr.getLogicalLineNumber(R.getEnd()); + unsigned EndLineNo = SourceMgr->getLogicalLineNumber(R.getEnd()); if (EndLineNo < LineNo) return; // No intersection. // Compute the column number of the start. unsigned StartColNo = 0; if (StartLineNo == LineNo) { - StartColNo = SourceMgr.getLogicalColumnNumber(R.getBegin()); + StartColNo = SourceMgr->getLogicalColumnNumber(R.getBegin()); if (StartColNo) --StartColNo; // Zero base the col #. } @@ -76,12 +76,12 @@ void TextDiagnosticPrinter::HighlightRange(const SourceRange &R, // Compute the column number of the end. unsigned EndColNo = CaratLine.size(); if (EndLineNo == LineNo) { - EndColNo = SourceMgr.getLogicalColumnNumber(R.getEnd()); + EndColNo = SourceMgr->getLogicalColumnNumber(R.getEnd()); if (EndColNo) { --EndColNo; // Zero base the col #. // Add in the length of the token, so that we cover multi-char tokens. - EndColNo += Lexer::MeasureTokenLength(R.getEnd(), SourceMgr); + EndColNo += Lexer::MeasureTokenLength(R.getEnd(), *SourceMgr); } else { EndColNo = CaratLine.size(); } @@ -102,7 +102,7 @@ void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic &Diags, Diagnostic::Level Level, SourceLocation Pos, diag::kind ID, - SourceManager& SourceMgr, + SourceManager* SourceMgr, const std::string *Strs, unsigned NumStrs, const SourceRange *Ranges, @@ -111,25 +111,25 @@ void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic &Diags, const char *LineStart = 0, *LineEnd = 0; if (Pos.isValid()) { - SourceLocation LPos = SourceMgr.getLogicalLoc(Pos); - LineNo = SourceMgr.getLineNumber(LPos); + SourceLocation LPos = SourceMgr->getLogicalLoc(Pos); + LineNo = SourceMgr->getLineNumber(LPos); // First, if this diagnostic is not in the main file, print out the // "included from" lines. - if (LastWarningLoc != SourceMgr.getIncludeLoc(LPos)) { - LastWarningLoc = SourceMgr.getIncludeLoc(LPos); - PrintIncludeStack(LastWarningLoc,SourceMgr); + if (LastWarningLoc != SourceMgr->getIncludeLoc(LPos)) { + LastWarningLoc = SourceMgr->getIncludeLoc(LPos); + PrintIncludeStack(LastWarningLoc,*SourceMgr); } // Compute the column number. Rewind from the current position to the start // of the line. - ColNo = SourceMgr.getColumnNumber(LPos); - const char *TokLogicalPtr = SourceMgr.getCharacterData(LPos); + ColNo = SourceMgr->getColumnNumber(LPos); + const char *TokLogicalPtr = SourceMgr->getCharacterData(LPos); LineStart = TokLogicalPtr-ColNo+1; // Column # is 1-based // Compute the line end. Scan forward from the error position to the end of // the line. - const llvm::MemoryBuffer *Buffer = SourceMgr.getBuffer(LPos.getFileID()); + const llvm::MemoryBuffer *Buffer = SourceMgr->getBuffer(LPos.getFileID()); const char *BufEnd = Buffer->getBufferEnd(); LineEnd = TokLogicalPtr; while (LineEnd != BufEnd && diff --git a/Driver/TextDiagnosticPrinter.h b/Driver/TextDiagnosticPrinter.h index c87c20e4f0..1f0a52ce71 100644 --- a/Driver/TextDiagnosticPrinter.h +++ b/Driver/TextDiagnosticPrinter.h @@ -28,7 +28,7 @@ public: void PrintIncludeStack(SourceLocation Pos, SourceManager& SrcMgr); void HighlightRange(const SourceRange &R, - SourceManager& SrcMgr, + SourceManager* SrcMgr, unsigned LineNo, std::string &CaratLine, const std::string &SourceLine); @@ -36,7 +36,7 @@ public: virtual void HandleDiagnostic(Diagnostic &Diags, Diagnostic::Level DiagLevel, SourceLocation Pos, diag::kind ID, - SourceManager& SrcMgr, + SourceManager* SrcMgr, const std::string *Strs, unsigned NumStrs, const SourceRange *Ranges, diff --git a/Driver/TextDiagnostics.cpp b/Driver/TextDiagnostics.cpp index 46e535de90..052ea6b98d 100644 --- a/Driver/TextDiagnostics.cpp +++ b/Driver/TextDiagnostics.cpp @@ -41,12 +41,12 @@ std::string TextDiagnostics::FormatDiagnostic(Diagnostic &Diags, bool TextDiagnostics::IgnoreDiagnostic(Diagnostic::Level Level, SourceLocation Pos, - SourceManager& SourceMgr) { + SourceManager* SourceMgr) { if (Pos.isValid()) { // If this is a warning or note, and if it a system header, suppress the // diagnostic. if (Level == Diagnostic::Warning || Level == Diagnostic::Note) { - if (const FileEntry *F = SourceMgr.getFileEntryForLoc(Pos)) { + if (const FileEntry *F = SourceMgr->getFileEntryForLoc(Pos)) { DirectoryLookup::DirType DirInfo = TheHeaderSearch->getFileDirFlavor(F); if (DirInfo == DirectoryLookup::SystemHeaderDir || DirInfo == DirectoryLookup::ExternCSystemHeaderDir) diff --git a/Driver/TextDiagnostics.h b/Driver/TextDiagnostics.h index ad5e4cb89b..7741ab99b8 100644 --- a/Driver/TextDiagnostics.h +++ b/Driver/TextDiagnostics.h @@ -36,12 +36,12 @@ public: virtual bool IgnoreDiagnostic(Diagnostic::Level Level, SourceLocation Pos, - SourceManager& SrcMgr); + SourceManager* SrcMgr); virtual void HandleDiagnostic(Diagnostic &Diags, Diagnostic::Level DiagLevel, SourceLocation Pos, diag::kind ID, - SourceManager& SrcMgr, + SourceManager* SrcMgr, const std::string *Strs, unsigned NumStrs, const SourceRange *Ranges, diff --git a/Driver/clang.cpp b/Driver/clang.cpp index c009e06860..f9b79d7736 100644 --- a/Driver/clang.cpp +++ b/Driver/clang.cpp @@ -1026,9 +1026,7 @@ int main(int argc, char **argv) { // -I- is a deprecated GCC feature, scan for it and reject it. for (unsigned i = 0, e = I_dirs.size(); i != e; ++i) { if (I_dirs[i] == "-") { - Diags.Report(SourceLocation(), diag::err_pp_I_dash_not_supported, - SourceMgr); - + Diags.Report(diag::err_pp_I_dash_not_supported); I_dirs.erase(I_dirs.begin()+i); --i; } diff --git a/include/clang/Basic/Diagnostic.h b/include/clang/Basic/Diagnostic.h index 5abc28f2e4..d5ca1203fc 100644 --- a/include/clang/Basic/Diagnostic.h +++ b/include/clang/Basic/Diagnostic.h @@ -14,12 +14,12 @@ #ifndef LLVM_CLANG_DIAGNOSTIC_H #define LLVM_CLANG_DIAGNOSTIC_H +#include "clang/Basic/SourceLocation.h" #include #include namespace clang { class DiagnosticClient; - class SourceLocation; class SourceRange; class SourceManager; @@ -149,7 +149,23 @@ public: /// diag::kind enum. void Report(SourceLocation Pos, unsigned DiagID, SourceManager& SrcMgr, const std::string *Strs = 0, unsigned NumStrs = 0, - const SourceRange *Ranges = 0, unsigned NumRanges = 0); + const SourceRange *Ranges = 0, unsigned NumRanges = 0) { + Report(Pos,DiagID,&SrcMgr,Strs,NumStrs,Ranges,NumRanges); + } + + + /// Report - Issue the message to the client. DiagID is a member of the + /// diag::kind enum. + void Report(unsigned DiagID, const std::string *Strs = 0, + unsigned NumStrs = 0, const SourceRange *Ranges = 0, + unsigned NumRanges = 0) { + Report(SourceLocation(),DiagID,NULL,Strs,NumStrs,Ranges,NumRanges); + } + +private: + void Report(SourceLocation Pos, unsigned DiagID, SourceManager* SrcMgr, + const std::string *Strs, unsigned NumStrs, + const SourceRange *Ranges, unsigned NumRanges); }; /// DiagnosticClient - This is an abstract interface implemented by clients of @@ -162,13 +178,13 @@ public: /// return true. virtual bool IgnoreDiagnostic(Diagnostic::Level DiagLevel, SourceLocation Pos, - SourceManager& SrcMgr) = 0; + SourceManager* SrcMgr) = 0; /// HandleDiagnostic - Handle this diagnostic, reporting it to the user or /// capturing it to a log as needed. virtual void HandleDiagnostic(Diagnostic &Diags, Diagnostic::Level DiagLevel, SourceLocation Pos, - diag::kind ID, SourceManager& SrcMgr, + diag::kind ID, SourceManager* SrcMgr, const std::string *Strs, unsigned NumStrs, const SourceRange *Ranges, unsigned NumRanges) = 0; -- 2.40.0