From: Chris Lattner Date: Sun, 15 Feb 2009 21:26:50 +0000 (+0000) Subject: add a new SourceManager::getInstantiationRange helper method. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6678133b8ce642f93e5141f056fa643112041ad0;p=clang add a new SourceManager::getInstantiationRange helper method. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@64606 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/SourceManager.h b/include/clang/Basic/SourceManager.h index d5845baa31..b29a1acd4a 100644 --- a/include/clang/Basic/SourceManager.h +++ b/include/clang/Basic/SourceManager.h @@ -425,8 +425,8 @@ public: return SourceLocation::getFileLoc(FileOffset); } - /// Given a SourceLocation object, return the instantiation location - /// referenced by the ID. + /// getInstantiationLoc - Given a SourceLocation object, return the + /// instantiation location referenced by the ID. SourceLocation getInstantiationLoc(SourceLocation Loc) const { // Handle the non-mapped case inline, defer to out of line code to handle // instantiations. @@ -439,6 +439,12 @@ public: std::pair getImmediateInstantiationRange(SourceLocation Loc) const; + /// getInstantiationRange - Given a SourceLocation object, return the + /// range of tokens covered by the instantiation in the ultimate file. + std::pair + getInstantiationRange(SourceLocation Loc) const; + + /// getSpellingLoc - Given a SourceLocation object, return the spelling /// location referenced by the ID. This is the place where the characters /// that make up the lexed token can be found. diff --git a/lib/Basic/SourceManager.cpp b/lib/Basic/SourceManager.cpp index 71bda5b255..88980efcda 100644 --- a/lib/Basic/SourceManager.cpp +++ b/lib/Basic/SourceManager.cpp @@ -606,6 +606,24 @@ SourceManager::getImmediateInstantiationRange(SourceLocation Loc) const { return II.getInstantiationLocRange(); } +/// getInstantiationRange - Given a SourceLocation object, return the +/// range of tokens covered by the instantiation in the ultimate file. +std::pair +SourceManager::getInstantiationRange(SourceLocation Loc) const { + if (Loc.isFileID()) return std::make_pair(Loc, Loc); + + std::pair Res = + getImmediateInstantiationRange(Loc); + + // Fully resolve the start and end locations to their ultimate instantiation + // points. + while (!Res.first.isFileID()) + Res.first = getImmediateInstantiationRange(Res.first).first; + while (!Res.second.isFileID()) + Res.second = getImmediateInstantiationRange(Res.second).second; + return Res; +} + //===----------------------------------------------------------------------===// diff --git a/lib/Lex/PPMacroExpansion.cpp b/lib/Lex/PPMacroExpansion.cpp index 33b5ef8656..a8b09e5ede 100644 --- a/lib/Lex/PPMacroExpansion.cpp +++ b/lib/Lex/PPMacroExpansion.cpp @@ -471,9 +471,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) { // can matter for a function-like macro that expands to contain __LINE__. // Skip down through instantiation points until we find a file loc for the // end of the instantiation history. - while (!Loc.isFileID()) - Loc = SourceMgr.getImmediateInstantiationRange(Loc).second; - + Loc = SourceMgr.getInstantiationRange(Loc).second; PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc); // __LINE__ expands to a simple numeric value. Add a space after it so that