[cfe-commits] r142128 - /cfe/trunk/lib/Frontend/TextDiagnostic.cpp

Abramo Bagnara abramo.bagnara at gmail.com
Sun Oct 16 06:45:45 PDT 2011


Il 16/10/2011 11:39, Chandler Carruth ha scritto:
> Author: chandlerc
> Date: Sun Oct 16 04:39:09 2011
> New Revision: 142128
> 
> URL: http://llvm.org/viewvc/llvm-project?rev=142128&view=rev
> Log:
> Hoist the logic I added to compute the macro name into a helper
> function. No functionality changed.
> 
> Modified:
>     cfe/trunk/lib/Frontend/TextDiagnostic.cpp
> 
> Modified: cfe/trunk/lib/Frontend/TextDiagnostic.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/TextDiagnostic.cpp?rev=142128&r1=142127&r2=142128&view=diff
> ==============================================================================
> --- cfe/trunk/lib/Frontend/TextDiagnostic.cpp (original)
> +++ cfe/trunk/lib/Frontend/TextDiagnostic.cpp Sun Oct 16 04:39:09 2011
> @@ -389,6 +389,35 @@
>    return Wrapped;
>  }
>  
> +/// \brief Retrieve the name of the immediate macro expansion.
> +///
> +/// This routine starts from a source location, and finds the name of the macro
> +/// responsible for its immediate expansion. It looks through any intervening
> +/// macro argument expansions to compute this. It returns a StringRef which
> +/// refers to the SourceManager-owned buffer of the source where that macro
> +/// name is spelled. Thus, the result shouldn't out-live that SourceManager.
> +///
> +static StringRef getImmediateMacroName(SourceLocation Loc,
> +                                       const SourceManager &SM,
> +                                       const LangOptions &LangOpts) {
> +  assert(Loc.isMacroID() && "Only reasonble to call this on macros");
> +  // Walk past macro argument expanions.
> +  while (SM.isMacroArgExpansion(Loc))
> +    Loc = SM.getImmediateExpansionRange(Loc).first;
> +
> +  // Find the spelling location of the start of the non-argument expansion
> +  // range. This is where the macro name was spelled in order to begin
> +  // expanding this macro.
> +  Loc = SM.getSpellingLoc(SM.getImmediateExpansionRange(Loc).first);
> +
> +  // Dig out the buffer where the macro name was spelled and the extents of the
> +  // name so that we can render it into the expansion note.
> +  std::pair<FileID, unsigned> ExpansionInfo = SM.getDecomposedLoc(Loc);
> +  unsigned MacroTokenLength = Lexer::MeasureTokenLength(Loc, SM, LangOpts);
> +  StringRef ExpansionBuffer = SM.getBufferData(ExpansionInfo.first);
> +  return ExpansionBuffer.substr(ExpansionInfo.second, MacroTokenLength);
> +}
> +

Can you write this as a public SourceManager method?

-- 
Abramo Bagnara

Opera Unica                          Phone: +39.0546.656023
Via Borghesi, 16
48014 Castel Bolognese (RA) - Italy



More information about the cfe-commits mailing list