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/lib/ARCMigrate/TransformActions.cpp b/lib/ARCMigrate/TransformActions.cpp
index 2ae9798..3e49942 100644
--- a/lib/ARCMigrate/TransformActions.cpp
+++ b/lib/ARCMigrate/TransformActions.cpp
@@ -388,7 +388,7 @@
if (loc.isFileID())
return true;
- return SM.isAtStartOfMacroInstantiation(loc, Ctx.getLangOptions());
+ return PP.isAtStartOfMacroInstantiation(loc);
}
bool TransformActionsImpl::canInsertAfterToken(SourceLocation loc) {
@@ -401,7 +401,7 @@
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 80c52d7..5468291 100644
--- a/lib/ARCMigrate/Transforms.cpp
+++ b/lib/ARCMigrate/Transforms.cpp
@@ -37,7 +37,7 @@
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 137da0d..cd098b2 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 @@
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<FileID, unsigned> 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<FileID, unsigned> 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 6d25d2b..aabba0a 100644
--- a/lib/Lex/Lexer.cpp
+++ b/lib/Lex/Lexer.cpp
@@ -683,7 +683,7 @@
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 @@
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<FileID, unsigned> 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 f8652e0..7df43d7 100644
--- a/lib/Parse/ParseExpr.cpp
+++ b/lib/Parse/ParseExpr.cpp
@@ -310,8 +310,7 @@
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 =