From: Matt Beaumont-Gay Date: Mon, 18 Jun 2012 20:12:05 +0000 (+0000) Subject: Move a few static functions from DiagnosticRenderer.cpp into SourceManager. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=29271fbcef645117df05d5b60e593acb6562422c;p=clang Move a few static functions from DiagnosticRenderer.cpp into SourceManager. This simplifies the code a little bit, since these functions all took a SourceManager parameter and called a bunch of methods on it, and makes the functions available to other users. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@158676 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/SourceManager.h b/include/clang/Basic/SourceManager.h index 22d4ec737b..8eb386b8dc 100644 --- a/include/clang/Basic/SourceManager.h +++ b/include/clang/Basic/SourceManager.h @@ -1378,6 +1378,65 @@ public: return !isLoadedFileID(FID); } + /// Get a presumed location suitable for displaying in a diagnostic message, + /// taking into account macro arguments and expansions. + PresumedLoc getPresumedLocForDisplay(SourceLocation Loc) const { + // This is a condensed form of the algorithm used by emitCaretDiagnostic to + // walk to the top of the macro call stack. + while (Loc.isMacroID()) { + Loc = skipToMacroArgExpansion(Loc); + Loc = getImmediateMacroCallerLoc(Loc); + } + + return getPresumedLoc(Loc); + } + + /// Look through spelling locations for a macro argument expansion, and if + /// found skip to it so that we can trace the argument rather than the macros + /// in which that argument is used. If no macro argument expansion is found, + /// don't skip anything and return the starting location. + SourceLocation skipToMacroArgExpansion(SourceLocation StartLoc) const { + for (SourceLocation L = StartLoc; L.isMacroID(); + L = getImmediateSpellingLoc(L)) { + if (isMacroArgExpansion(L)) + return L; + } + // Otherwise just return initial location, there's nothing to skip. + return StartLoc; + } + + /// Gets the location of the immediate macro caller, one level up the stack + /// toward the initial macro typed into the source. + SourceLocation getImmediateMacroCallerLoc(SourceLocation Loc) const { + if (!Loc.isMacroID()) return Loc; + + // When we have the location of (part of) an expanded parameter, its + // spelling location points to the argument as typed into the macro call, + // and therefore is used to locate the macro caller. + if (isMacroArgExpansion(Loc)) + return getImmediateSpellingLoc(Loc); + + // Otherwise, the caller of the macro is located where this macro is + // expanded (while the spelling is part of the macro definition). + return getImmediateExpansionRange(Loc).first; + } + + /// Gets the location of the immediate macro callee, one level down the stack + /// toward the leaf macro. + SourceLocation getImmediateMacroCalleeLoc(SourceLocation Loc) const { + if (!Loc.isMacroID()) return Loc; + + // When we have the location of (part of) an expanded parameter, its + // expansion location points to the unexpanded parameter reference within + // the macro definition (or callee). + if (isMacroArgExpansion(Loc)) + return getImmediateExpansionRange(Loc).first; + + // Otherwise, the callee of the macro is located where this location was + // spelled inside the macro definition. + return getImmediateSpellingLoc(Loc); + } + private: const llvm::MemoryBuffer *getFakeBufferForRecovery() const; const SrcMgr::ContentCache *getFakeContentCacheForRecovery() const; diff --git a/lib/Frontend/DiagnosticRenderer.cpp b/lib/Frontend/DiagnosticRenderer.cpp index 7249943d3a..f052f90fa9 100644 --- a/lib/Frontend/DiagnosticRenderer.cpp +++ b/lib/Frontend/DiagnosticRenderer.cpp @@ -22,56 +22,6 @@ #include using namespace clang; -/// Look through spelling locations for a macro argument expansion, and -/// if found skip to it so that we can trace the argument rather than the macros -/// in which that argument is used. If no macro argument expansion is found, -/// don't skip anything and return the starting location. -static SourceLocation skipToMacroArgExpansion(const SourceManager &SM, - SourceLocation StartLoc) { - for (SourceLocation L = StartLoc; L.isMacroID(); - L = SM.getImmediateSpellingLoc(L)) { - if (SM.isMacroArgExpansion(L)) - return L; - } - - // Otherwise just return initial location, there's nothing to skip. - return StartLoc; -} - -/// Gets the location of the immediate macro caller, one level up the stack -/// toward the initial macro typed into the source. -static SourceLocation getImmediateMacroCallerLoc(const SourceManager &SM, - SourceLocation Loc) { - if (!Loc.isMacroID()) return Loc; - - // When we have the location of (part of) an expanded parameter, its spelling - // location points to the argument as typed into the macro call, and - // therefore is used to locate the macro caller. - if (SM.isMacroArgExpansion(Loc)) - return SM.getImmediateSpellingLoc(Loc); - - // Otherwise, the caller of the macro is located where this macro is - // expanded (while the spelling is part of the macro definition). - return SM.getImmediateExpansionRange(Loc).first; -} - -/// Gets the location of the immediate macro callee, one level down the stack -/// toward the leaf macro. -static SourceLocation getImmediateMacroCalleeLoc(const SourceManager &SM, - SourceLocation Loc) { - if (!Loc.isMacroID()) return Loc; - - // When we have the location of (part of) an expanded parameter, its - // expansion location points to the unexpanded paramater reference within - // the macro definition (or callee). - if (SM.isMacroArgExpansion(Loc)) - return SM.getImmediateExpansionRange(Loc).first; - - // Otherwise, the callee of the macro is located where this location was - // spelled inside the macro definition. - return SM.getImmediateSpellingLoc(Loc); -} - /// \brief Retrieve the name of the immediate macro expansion. /// /// This routine starts from a source location, and finds the name of the macro @@ -109,20 +59,6 @@ static StringRef getImmediateMacroName(SourceLocation Loc, return ExpansionBuffer.substr(ExpansionInfo.second, MacroTokenLength); } -/// Get the presumed location of a diagnostic message. This computes the -/// presumed location for the top of any macro backtrace when present. -static PresumedLoc getDiagnosticPresumedLoc(const SourceManager &SM, - SourceLocation Loc) { - // This is a condensed form of the algorithm used by emitCaretDiagnostic to - // walk to the top of the macro call stack. - while (Loc.isMacroID()) { - Loc = skipToMacroArgExpansion(SM, Loc); - Loc = getImmediateMacroCallerLoc(SM, Loc); - } - - return SM.getPresumedLoc(Loc); -} - DiagnosticRenderer::DiagnosticRenderer(const LangOptions &LangOpts, const DiagnosticOptions &DiagOpts) : LangOpts(LangOpts), DiagOpts(DiagOpts), LastLevel() {} @@ -191,7 +127,7 @@ void DiagnosticRenderer::emitDiagnostic(SourceLocation Loc, PresumedLoc PLoc; if (Loc.isValid()) { - PLoc = getDiagnosticPresumedLoc(*SM, Loc); + PLoc = SM->getPresumedLocForDisplay(Loc); // First, if this diagnostic is not in the main file, print out the // "included from" lines. @@ -318,9 +254,9 @@ void DiagnosticRenderer::emitMacroExpansionsAndCarets( // When processing macros, skip over the expansions leading up to // a macro argument, and trace the argument's expansion stack instead. - Loc = skipToMacroArgExpansion(SM, Loc); + Loc = SM.skipToMacroArgExpansion(Loc); - SourceLocation OneLevelUp = getImmediateMacroCallerLoc(SM, Loc); + SourceLocation OneLevelUp = SM.getImmediateMacroCallerLoc(Loc); // FIXME: Map ranges? emitMacroExpansionsAndCarets(OneLevelUp, Level, Ranges, Hints, SM, MacroDepth, @@ -330,7 +266,7 @@ void DiagnosticRenderer::emitMacroExpansionsAndCarets( SourceLocation MacroLoc = Loc; // Map the location. - Loc = getImmediateMacroCalleeLoc(SM, Loc); + Loc = SM.getImmediateMacroCalleeLoc(Loc); unsigned MacroSkipStart = 0, MacroSkipEnd = 0; if (MacroDepth > DiagOpts.MacroBacktraceLimit && @@ -350,9 +286,9 @@ void DiagnosticRenderer::emitMacroExpansionsAndCarets( I != E; ++I) { SourceLocation Start = I->getBegin(), End = I->getEnd(); if (Start.isMacroID()) - I->setBegin(getImmediateMacroCalleeLoc(SM, Start)); + I->setBegin(SM.getImmediateMacroCalleeLoc(Start)); if (End.isMacroID()) - I->setEnd(getImmediateMacroCalleeLoc(SM, End)); + I->setEnd(SM.getImmediateMacroCalleeLoc(End)); } if (Suppressed) { @@ -393,4 +329,3 @@ void DiagnosticNoteRenderer::emitIncludeLocation(SourceLocation Loc, void DiagnosticNoteRenderer::emitBasicNote(StringRef Message) { emitNote(SourceLocation(), Message, 0); } -