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.
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,
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;
if (loc.isFileID())
return true;
- return SM.isAtStartOfMacroInstantiation(loc, Ctx.getLangOptions());
+ return PP.isAtStartOfMacroInstantiation(loc);
}
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) {
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;
}
//
//===----------------------------------------------------------------------===//
-#include "clang/Lex/Lexer.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Basic/SourceManagerInternals.h"
#include "clang/Basic/Diagnostic.h"
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.
//===----------------------------------------------------------------------===//
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.
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.
//===----------------------------------------------------------------------===//
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 =