From: Argyrios Kyrtzidis Date: Thu, 7 Jul 2011 21:54:45 +0000 (+0000) Subject: Move SourceManager::isAt[Start/End]OfMacroInstantiation functions to the Lexer, since... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7a759606d93975866051f67104ae58446e55f404;p=clang Move SourceManager::isAt[Start/End]OfMacroInstantiation functions to the Lexer, since they depend on it now. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@134644 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/SourceManager.h b/include/clang/Basic/SourceManager.h index 33c66e7f7c..bd8f5ef6ef 100644 --- a/include/clang/Basic/SourceManager.h +++ b/include/clang/Basic/SourceManager.h @@ -832,16 +832,6 @@ public: return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem; } - /// \brief Returns true if the given MacroID location points at the first - /// token of the macro instantiation. - bool isAtStartOfMacroInstantiation(SourceLocation Loc, - const LangOptions &LangOpts) const; - - /// \brief Returns true if the given MacroID location points at the last - /// token of the macro instantiation. - bool isAtEndOfMacroInstantiation(SourceLocation Loc, - const LangOptions &LangOpts) const; - /// \brief Given a specific chunk of a FileID (FileID with offset+length), /// returns true if \arg Loc is inside that chunk and sets relative offset /// (offset of \arg Loc from beginning of chunk) to \arg relativeOffset. diff --git a/include/clang/Lex/Lexer.h b/include/clang/Lex/Lexer.h index 7c3d863bd3..937e468034 100644 --- a/include/clang/Lex/Lexer.h +++ b/include/clang/Lex/Lexer.h @@ -293,7 +293,19 @@ public: static SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset, const SourceManager &SM, const LangOptions &Features); - + + /// \brief Returns true if the given MacroID location points at the first + /// token of the macro instantiation. + static bool isAtStartOfMacroInstantiation(SourceLocation loc, + const SourceManager &SM, + const LangOptions &LangOpts); + + /// \brief Returns true if the given MacroID location points at the last + /// token of the macro instantiation. + static bool isAtEndOfMacroInstantiation(SourceLocation loc, + const SourceManager &SM, + const LangOptions &LangOpts); + /// \brief Compute the preamble of the given file. /// /// The preamble of a file contains the initial comments, include directives, diff --git a/include/clang/Lex/Preprocessor.h b/include/clang/Lex/Preprocessor.h index 56fc9d1727..e1ea37f241 100644 --- a/include/clang/Lex/Preprocessor.h +++ b/include/clang/Lex/Preprocessor.h @@ -753,6 +753,18 @@ public: return Lexer::getLocForEndOfToken(Loc, Offset, SourceMgr, Features); } + /// \brief Returns true if the given MacroID location points at the first + /// token of the macro instantiation. + bool isAtStartOfMacroInstantiation(SourceLocation loc) const { + return Lexer::isAtStartOfMacroInstantiation(loc, SourceMgr, Features); + } + + /// \brief Returns true if the given MacroID location points at the last + /// token of the macro instantiation. + bool isAtEndOfMacroInstantiation(SourceLocation loc) const { + return Lexer::isAtEndOfMacroInstantiation(loc, SourceMgr, Features); + } + /// DumpToken - Print the token to stderr, used for debugging. /// void DumpToken(const Token &Tok, bool DumpFlags = false) const; diff --git a/lib/ARCMigrate/TransformActions.cpp b/lib/ARCMigrate/TransformActions.cpp index 2ae979896a..3e49942788 100644 --- a/lib/ARCMigrate/TransformActions.cpp +++ b/lib/ARCMigrate/TransformActions.cpp @@ -388,7 +388,7 @@ bool TransformActionsImpl::canInsert(SourceLocation loc) { if (loc.isFileID()) return true; - return SM.isAtStartOfMacroInstantiation(loc, Ctx.getLangOptions()); + return PP.isAtStartOfMacroInstantiation(loc); } bool TransformActionsImpl::canInsertAfterToken(SourceLocation loc) { @@ -401,7 +401,7 @@ bool TransformActionsImpl::canInsertAfterToken(SourceLocation loc) { if (loc.isFileID()) return true; - return SM.isAtEndOfMacroInstantiation(loc, Ctx.getLangOptions()); + return PP.isAtEndOfMacroInstantiation(loc); } bool TransformActionsImpl::canRemoveRange(SourceRange range) { diff --git a/lib/ARCMigrate/Transforms.cpp b/lib/ARCMigrate/Transforms.cpp index 80c52d7940..546829120c 100644 --- a/lib/ARCMigrate/Transforms.cpp +++ b/lib/ARCMigrate/Transforms.cpp @@ -37,7 +37,7 @@ SourceLocation trans::findLocationAfterSemi(SourceLocation loc, ASTContext &Ctx) { SourceManager &SM = Ctx.getSourceManager(); if (loc.isMacroID()) { - if (!SM.isAtEndOfMacroInstantiation(loc, Ctx.getLangOptions())) + if (!Lexer::isAtEndOfMacroInstantiation(loc, SM, Ctx.getLangOptions())) return SourceLocation(); loc = SM.getInstantiationRange(loc).second; } diff --git a/lib/Basic/SourceManager.cpp b/lib/Basic/SourceManager.cpp index 137da0d87a..cd098b23aa 100644 --- a/lib/Basic/SourceManager.cpp +++ b/lib/Basic/SourceManager.cpp @@ -11,7 +11,6 @@ // //===----------------------------------------------------------------------===// -#include "clang/Lex/Lexer.h" #include "clang/Basic/SourceManager.h" #include "clang/Basic/SourceManagerInternals.h" #include "clang/Basic/Diagnostic.h" @@ -1215,60 +1214,6 @@ PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc) const { return PresumedLoc(Filename, LineNo, ColNo, IncludeLoc); } -/// \brief Returns true if the given MacroID location points at the first -/// token of the macro instantiation. -bool SourceManager::isAtStartOfMacroInstantiation(SourceLocation loc, - const LangOptions &LangOpts) const { - assert(loc.isValid() && loc.isMacroID() && "Expected a valid macro loc"); - - std::pair infoLoc = getDecomposedLoc(loc); - // FIXME: If the token comes from the macro token paste operator ('##') - // this function will always return false; - if (infoLoc.second > 0) - return false; // Does not point at the start of token. - - SourceLocation instLoc = - getSLocEntry(infoLoc.first).getInstantiation().getInstantiationLocStart(); - if (instLoc.isFileID()) - return true; // No other macro instantiations, this is the first. - - return isAtStartOfMacroInstantiation(instLoc, LangOpts); -} - -/// \brief Returns true if the given MacroID location points at the last -/// token of the macro instantiation. -bool SourceManager::isAtEndOfMacroInstantiation(SourceLocation loc, - const LangOptions &LangOpts) const { - assert(loc.isValid() && loc.isMacroID() && "Expected a valid macro loc"); - - SourceLocation spellLoc = getSpellingLoc(loc); - unsigned tokLen = Lexer::MeasureTokenLength(spellLoc, *this, LangOpts); - if (tokLen == 0) - return false; - - std::pair infoLoc = getDecomposedLoc(loc); - unsigned FID = infoLoc.first.ID; - - unsigned NextOffset; - if (FID+1 == sloc_entry_size()) - NextOffset = getNextOffset(); - else - NextOffset = getSLocEntry(FID+1).getOffset(); - - // FIXME: If the token comes from the macro token paste operator ('##') - // or the stringify operator ('#') this function will always return false; - assert(loc.getOffset() + tokLen < NextOffset); - if (loc.getOffset() + tokLen < NextOffset-1) - return false; // Does not point to the last token. - - SourceLocation instLoc = - getSLocEntry(infoLoc.first).getInstantiation().getInstantiationLocEnd(); - if (instLoc.isFileID()) - return true; // No other macro instantiations. - - return isAtEndOfMacroInstantiation(instLoc, LangOpts); -} - //===----------------------------------------------------------------------===// // Other miscellaneous methods. //===----------------------------------------------------------------------===// diff --git a/lib/Lex/Lexer.cpp b/lib/Lex/Lexer.cpp index 6d25d2b2cf..aabba0aa56 100644 --- a/lib/Lex/Lexer.cpp +++ b/lib/Lex/Lexer.cpp @@ -683,7 +683,7 @@ SourceLocation Lexer::getLocForEndOfToken(SourceLocation Loc, unsigned Offset, return SourceLocation(); if (Loc.isMacroID()) { - if (Offset > 0 || !SM.isAtEndOfMacroInstantiation(Loc, Features)) + if (Offset > 0 || !isAtEndOfMacroInstantiation(Loc, SM, Features)) return SourceLocation(); // Points inside the macro instantiation. // Continue and find the location just after the macro instantiation. @@ -699,6 +699,57 @@ SourceLocation Lexer::getLocForEndOfToken(SourceLocation Loc, unsigned Offset, return Loc.getFileLocWithOffset(Len); } +/// \brief Returns true if the given MacroID location points at the first +/// token of the macro instantiation. +bool Lexer::isAtStartOfMacroInstantiation(SourceLocation loc, + const SourceManager &SM, + const LangOptions &LangOpts) { + assert(loc.isValid() && loc.isMacroID() && "Expected a valid macro loc"); + + std::pair infoLoc = SM.getDecomposedLoc(loc); + // FIXME: If the token comes from the macro token paste operator ('##') + // this function will always return false; + if (infoLoc.second > 0) + return false; // Does not point at the start of token. + + SourceLocation instLoc = + SM.getSLocEntry(infoLoc.first).getInstantiation().getInstantiationLocStart(); + if (instLoc.isFileID()) + return true; // No other macro instantiations, this is the first. + + return isAtStartOfMacroInstantiation(instLoc, SM, LangOpts); +} + +/// \brief Returns true if the given MacroID location points at the last +/// token of the macro instantiation. +bool Lexer::isAtEndOfMacroInstantiation(SourceLocation loc, + const SourceManager &SM, + const LangOptions &LangOpts) { + assert(loc.isValid() && loc.isMacroID() && "Expected a valid macro loc"); + + SourceLocation spellLoc = SM.getSpellingLoc(loc); + unsigned tokLen = MeasureTokenLength(spellLoc, SM, LangOpts); + if (tokLen == 0) + return false; + + FileID FID = SM.getFileID(loc); + SourceLocation afterLoc = loc.getFileLocWithOffset(tokLen+1); + if (!SM.isBeforeInSourceLocationOffset(afterLoc, SM.getNextOffset())) + return true; // We got past the last FileID, this points to the last token. + + // FIXME: If the token comes from the macro token paste operator ('##') + // or the stringify operator ('#') this function will always return false; + if (FID == SM.getFileID(afterLoc)) + return false; // Still in the same FileID, does not point to the last token. + + SourceLocation instLoc = + SM.getSLocEntry(FID).getInstantiation().getInstantiationLocEnd(); + if (instLoc.isFileID()) + return true; // No other macro instantiations. + + return isAtEndOfMacroInstantiation(instLoc, SM, LangOpts); +} + //===----------------------------------------------------------------------===// // Character information. //===----------------------------------------------------------------------===// diff --git a/lib/Parse/ParseExpr.cpp b/lib/Parse/ParseExpr.cpp index f8652e0074..7df43d7874 100644 --- a/lib/Parse/ParseExpr.cpp +++ b/lib/Parse/ParseExpr.cpp @@ -310,8 +310,7 @@ Parser::ParseRHSOfBinaryExpression(ExprResult LHS, prec::Level MinPrec) { SourceLocation FILoc = Tok.getLocation(); const char *FIText = ": "; const SourceManager &SM = PP.getSourceManager(); - if (FILoc.isFileID() || - SM.isAtStartOfMacroInstantiation(FILoc, getLang())) { + if (FILoc.isFileID() || PP.isAtStartOfMacroInstantiation(FILoc)) { FILoc = SM.getInstantiationLoc(FILoc); bool IsInvalid = false; const char *SourcePtr =