本文整理汇总了C++中PresumedLoc::isInvalid方法的典型用法代码示例。如果您正苦于以下问题:C++ PresumedLoc::isInvalid方法的具体用法?C++ PresumedLoc::isInvalid怎么用?C++ PresumedLoc::isInvalid使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PresumedLoc
的用法示例。
在下文中一共展示了PresumedLoc::isInvalid方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: ExpandBuiltinMacro
/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
/// as a builtin macro, handle it and return the next token as 'Tok'.
void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
// Figure out which token this is.
IdentifierInfo *II = Tok.getIdentifierInfo();
assert(II && "Can't be a macro without id info!");
// If this is an _Pragma or Microsoft __pragma directive, expand it,
// invoke the pragma handler, then lex the token after it.
if (II == Ident_Pragma)
return Handle_Pragma(Tok);
else if (II == Ident__pragma) // in non-MS mode this is null
return HandleMicrosoft__pragma(Tok);
++NumBuiltinMacroExpanded;
llvm::SmallString<128> TmpBuffer;
llvm::raw_svector_ostream OS(TmpBuffer);
// Set up the return result.
Tok.setIdentifierInfo(0);
Tok.clearFlag(Token::NeedsCleaning);
if (II == Ident__LINE__) {
// C99 6.10.8: "__LINE__: The presumed line number (within the current
// source file) of the current source line (an integer constant)". This can
// be affected by #line.
SourceLocation Loc = Tok.getLocation();
// Advance to the location of the first _, this might not be the first byte
// of the token if it starts with an escaped newline.
Loc = AdvanceToTokenCharacter(Loc, 0);
// One wrinkle here is that GCC expands __LINE__ to location of the *end* of
// a macro expansion. This doesn't matter for object-like macros, but
// can matter for a function-like macro that expands to contain __LINE__.
// Skip down through expansion points until we find a file loc for the
// end of the expansion history.
Loc = SourceMgr.getExpansionRange(Loc).second;
PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc);
// __LINE__ expands to a simple numeric value.
OS << (PLoc.isValid()? PLoc.getLine() : 1);
Tok.setKind(tok::numeric_constant);
} else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
// C99 6.10.8: "__FILE__: The presumed name of the current source file (a
// character string literal)". This can be affected by #line.
PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
// __BASE_FILE__ is a GNU extension that returns the top of the presumed
// #include stack instead of the current file.
if (II == Ident__BASE_FILE__ && PLoc.isValid()) {
SourceLocation NextLoc = PLoc.getIncludeLoc();
while (NextLoc.isValid()) {
PLoc = SourceMgr.getPresumedLoc(NextLoc);
if (PLoc.isInvalid())
break;
NextLoc = PLoc.getIncludeLoc();
}
}
// Escape this filename. Turn '\' -> '\\' '"' -> '\"'
llvm::SmallString<128> FN;
if (PLoc.isValid()) {
FN += PLoc.getFilename();
Lexer::Stringify(FN);
OS << '"' << FN.str() << '"';
}
Tok.setKind(tok::string_literal);
} else if (II == Ident__DATE__) {
if (!DATELoc.isValid())
ComputeDATE_TIME(DATELoc, TIMELoc, *this);
Tok.setKind(tok::string_literal);
Tok.setLength(strlen("\"Mmm dd yyyy\""));
Tok.setLocation(SourceMgr.createExpansionLoc(DATELoc, Tok.getLocation(),
Tok.getLocation(),
Tok.getLength()));
return;
} else if (II == Ident__TIME__) {
if (!TIMELoc.isValid())
ComputeDATE_TIME(DATELoc, TIMELoc, *this);
Tok.setKind(tok::string_literal);
Tok.setLength(strlen("\"hh:mm:ss\""));
Tok.setLocation(SourceMgr.createExpansionLoc(TIMELoc, Tok.getLocation(),
Tok.getLocation(),
Tok.getLength()));
return;
} else if (II == Ident__INCLUDE_LEVEL__) {
// Compute the presumed include depth of this token. This can be affected
// by GNU line markers.
unsigned Depth = 0;
PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
if (PLoc.isValid()) {
PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
for (; PLoc.isValid(); ++Depth)
PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
}
//.........这里部分代码省略.........
示例2: emitDiagnosticLoc
/// \brief Print out the file/line/column information and include trace.
///
/// This method handlen the emission of the diagnostic location information.
/// This includes extracting as much location information as is present for
/// the diagnostic and printing it, as well as any include stack or source
/// ranges necessary.
void TextDiagnostic::emitDiagnosticLoc(SourceLocation Loc, PresumedLoc PLoc,
DiagnosticsEngine::Level Level,
ArrayRef<CharSourceRange> Ranges) {
if (PLoc.isInvalid()) {
// At least print the file name if available:
FileID FID = SM.getFileID(Loc);
if (!FID.isInvalid()) {
const FileEntry* FE = SM.getFileEntryForID(FID);
if (FE && FE->getName()) {
OS << FE->getName();
if (FE->getDevice() == 0 && FE->getInode() == 0
&& FE->getFileMode() == 0) {
// in PCH is a guess, but a good one:
OS << " (in PCH)";
}
OS << ": ";
}
}
return;
}
unsigned LineNo = PLoc.getLine();
if (!DiagOpts.ShowLocation)
return;
if (DiagOpts.ShowColors)
OS.changeColor(savedColor, true);
OS << PLoc.getFilename();
switch (DiagOpts.Format) {
case DiagnosticOptions::Clang: OS << ':' << LineNo; break;
case DiagnosticOptions::Msvc: OS << '(' << LineNo; break;
case DiagnosticOptions::Vi: OS << " +" << LineNo; break;
}
if (DiagOpts.ShowColumn)
// Compute the column number.
if (unsigned ColNo = PLoc.getColumn()) {
if (DiagOpts.Format == DiagnosticOptions::Msvc) {
OS << ',';
ColNo--;
} else
OS << ':';
OS << ColNo;
}
switch (DiagOpts.Format) {
case DiagnosticOptions::Clang:
case DiagnosticOptions::Vi: OS << ':'; break;
case DiagnosticOptions::Msvc: OS << ") : "; break;
}
if (DiagOpts.ShowSourceRanges && !Ranges.empty()) {
FileID CaretFileID =
SM.getFileID(SM.getExpansionLoc(Loc));
bool PrintedRange = false;
for (ArrayRef<CharSourceRange>::const_iterator RI = Ranges.begin(),
RE = Ranges.end();
RI != RE; ++RI) {
// Ignore invalid ranges.
if (!RI->isValid()) continue;
SourceLocation B = SM.getExpansionLoc(RI->getBegin());
SourceLocation E = SM.getExpansionLoc(RI->getEnd());
// If the End location and the start location are the same and are a
// macro location, then the range was something that came from a
// macro expansion or _Pragma. If this is an object-like macro, the
// best we can do is to highlight the range. If this is a
// function-like macro, we'd also like to highlight the arguments.
if (B == E && RI->getEnd().isMacroID())
E = SM.getExpansionRange(RI->getEnd()).second;
std::pair<FileID, unsigned> BInfo = SM.getDecomposedLoc(B);
std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(E);
// If the start or end of the range is in another file, just discard
// it.
if (BInfo.first != CaretFileID || EInfo.first != CaretFileID)
continue;
// Add in the length of the token, so that we cover multi-char
// tokens.
unsigned TokSize = 0;
if (RI->isTokenRange())
TokSize = Lexer::MeasureTokenLength(E, SM, LangOpts);
OS << '{' << SM.getLineNumber(BInfo.first, BInfo.second) << ':'
<< SM.getColumnNumber(BInfo.first, BInfo.second) << '-'
<< SM.getLineNumber(EInfo.first, EInfo.second) << ':'
<< (SM.getColumnNumber(EInfo.first, EInfo.second)+TokSize)
<< '}';
PrintedRange = true;
}
//.........这里部分代码省略.........
示例3: emitDiagnosticLoc
/// \brief Print out the file/line/column information and include trace.
///
/// This method handlen the emission of the diagnostic location information.
/// This includes extracting as much location information as is present for
/// the diagnostic and printing it, as well as any include stack or source
/// ranges necessary.
void TextDiagnostic::emitDiagnosticLoc(SourceLocation Loc, PresumedLoc PLoc,
DiagnosticsEngine::Level Level,
ArrayRef<CharSourceRange> Ranges,
const SourceManager &SM) {
if (PLoc.isInvalid()) {
// At least print the file name if available:
FileID FID = SM.getFileID(Loc);
if (FID.isValid()) {
const FileEntry* FE = SM.getFileEntryForID(FID);
if (FE && FE->isValid()) {
emitFilename(FE->getName(), SM);
if (FE->isInPCH())
OS << " (in PCH)";
OS << ": ";
}
}
return;
}
unsigned LineNo = PLoc.getLine();
if (!DiagOpts->ShowLocation)
return;
if (DiagOpts->ShowColors)
OS.changeColor(savedColor, true);
emitFilename(PLoc.getFilename(), SM);
switch (DiagOpts->getFormat()) {
case DiagnosticOptions::Clang: OS << ':' << LineNo; break;
case DiagnosticOptions::MSVC: OS << '(' << LineNo; break;
case DiagnosticOptions::Vi: OS << " +" << LineNo; break;
}
if (DiagOpts->ShowColumn)
// Compute the column number.
if (unsigned ColNo = PLoc.getColumn()) {
if (DiagOpts->getFormat() == DiagnosticOptions::MSVC) {
OS << ',';
// Visual Studio 2010 or earlier expects column number to be off by one
if (LangOpts.MSCompatibilityVersion &&
!LangOpts.isCompatibleWithMSVC(LangOptions::MSVC2012))
ColNo--;
} else
OS << ':';
OS << ColNo;
}
switch (DiagOpts->getFormat()) {
case DiagnosticOptions::Clang:
case DiagnosticOptions::Vi: OS << ':'; break;
case DiagnosticOptions::MSVC:
// MSVC2013 and before print 'file(4) : error'. MSVC2015 gets rid of the
// space and prints 'file(4): error'.
OS << ')';
if (LangOpts.MSCompatibilityVersion &&
!LangOpts.isCompatibleWithMSVC(LangOptions::MSVC2015))
OS << ' ';
OS << ": ";
break;
}
if (DiagOpts->ShowSourceRanges && !Ranges.empty()) {
FileID CaretFileID =
SM.getFileID(SM.getExpansionLoc(Loc));
bool PrintedRange = false;
for (ArrayRef<CharSourceRange>::const_iterator RI = Ranges.begin(),
RE = Ranges.end();
RI != RE; ++RI) {
// Ignore invalid ranges.
if (!RI->isValid()) continue;
SourceLocation B = SM.getExpansionLoc(RI->getBegin());
SourceLocation E = SM.getExpansionLoc(RI->getEnd());
// If the End location and the start location are the same and are a
// macro location, then the range was something that came from a
// macro expansion or _Pragma. If this is an object-like macro, the
// best we can do is to highlight the range. If this is a
// function-like macro, we'd also like to highlight the arguments.
if (B == E && RI->getEnd().isMacroID())
E = SM.getExpansionRange(RI->getEnd()).second;
std::pair<FileID, unsigned> BInfo = SM.getDecomposedLoc(B);
std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(E);
// If the start or end of the range is in another file, just discard
// it.
if (BInfo.first != CaretFileID || EInfo.first != CaretFileID)
continue;
// Add in the length of the token, so that we cover multi-char
// tokens.
unsigned TokSize = 0;
if (RI->isTokenRange())
//.........这里部分代码省略.........
示例4: emitDiagnosticLoc
/// Print out the file/line/column information and include trace.
///
/// This method handlen the emission of the diagnostic location information.
/// This includes extracting as much location information as is present for
/// the diagnostic and printing it, as well as any include stack or source
/// ranges necessary.
void TextDiagnostic::emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc,
DiagnosticsEngine::Level Level,
ArrayRef<CharSourceRange> Ranges) {
if (PLoc.isInvalid()) {
// At least print the file name if available:
FileID FID = Loc.getFileID();
if (FID.isValid()) {
const FileEntry *FE = Loc.getFileEntry();
if (FE && FE->isValid()) {
emitFilename(FE->getName(), Loc.getManager());
if (FE->isInPCH())
OS << " (in PCH)";
OS << ": ";
}
}
return;
}
unsigned LineNo = PLoc.getLine();
if (!DiagOpts->ShowLocation)
return;
if (DiagOpts->ShowColors)
OS.changeColor(savedColor, true);
emitFilename(PLoc.getFilename(), Loc.getManager());
switch (DiagOpts->getFormat()) {
case DiagnosticOptions::Clang: OS << ':' << LineNo; break;
case DiagnosticOptions::MSVC: OS << '(' << LineNo; break;
case DiagnosticOptions::Vi: OS << " +" << LineNo; break;
}
if (DiagOpts->ShowColumn)
// Compute the column number.
if (unsigned ColNo = PLoc.getColumn()) {
if (DiagOpts->getFormat() == DiagnosticOptions::MSVC) {
OS << ',';
// Visual Studio 2010 or earlier expects column number to be off by one
if (LangOpts.MSCompatibilityVersion &&
!LangOpts.isCompatibleWithMSVC(LangOptions::MSVC2012))
ColNo--;
} else
OS << ':';
OS << ColNo;
}
switch (DiagOpts->getFormat()) {
case DiagnosticOptions::Clang:
case DiagnosticOptions::Vi: OS << ':'; break;
case DiagnosticOptions::MSVC:
// MSVC2013 and before print 'file(4) : error'. MSVC2015 gets rid of the
// space and prints 'file(4): error'.
OS << ')';
if (LangOpts.MSCompatibilityVersion &&
!LangOpts.isCompatibleWithMSVC(LangOptions::MSVC2015))
OS << ' ';
OS << ": ";
break;
}
if (DiagOpts->ShowSourceRanges && !Ranges.empty()) {
FileID CaretFileID = Loc.getExpansionLoc().getFileID();
bool PrintedRange = false;
for (ArrayRef<CharSourceRange>::const_iterator RI = Ranges.begin(),
RE = Ranges.end();
RI != RE; ++RI) {
// Ignore invalid ranges.
if (!RI->isValid()) continue;
auto &SM = Loc.getManager();
SourceLocation B = SM.getExpansionLoc(RI->getBegin());
CharSourceRange ERange = SM.getExpansionRange(RI->getEnd());
SourceLocation E = ERange.getEnd();
bool IsTokenRange = ERange.isTokenRange();
std::pair<FileID, unsigned> BInfo = SM.getDecomposedLoc(B);
std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(E);
// If the start or end of the range is in another file, just discard
// it.
if (BInfo.first != CaretFileID || EInfo.first != CaretFileID)
continue;
// Add in the length of the token, so that we cover multi-char
// tokens.
unsigned TokSize = 0;
if (IsTokenRange)
TokSize = Lexer::MeasureTokenLength(E, SM, LangOpts);
FullSourceLoc BF(B, SM), EF(E, SM);
OS << '{'
<< BF.getLineNumber() << ':' << BF.getColumnNumber() << '-'
<< EF.getLineNumber() << ':' << (EF.getColumnNumber() + TokSize)
<< '}';
//.........这里部分代码省略.........
示例5: finder
void C2Builder::rewriterTest(SourceManager& SM, LangOptions& LangOpts) {
#if 0
// FOR TESTING rename global test.aa -> bb
const std::string modName = "test";
const std::string oldName = "aa";
const std::string newName = "bb";
// Step 1a: find Module
const Module* M = 0;
const Module* mod = findModule(modName);
assert(M && "unknown module");
assert(!M->isExternal() && "cannot replace symbol in external module");
// Step 1b: find Decl
Decl* D = M->findSymbol(oldName);
assert(D && "unknown decl");
// Step 2a: replace Decl itself
Rewriter rewriter;
rewriter.setSourceMgr(SM, LangOpts);
rewriter.ReplaceText(D->getLocation(), oldName.size(), newName);
// Step 2b: replace all references
// TODO only in mainComponent
const ModuleList& mods = mainComponent->getModules();
for (unsigned m=0; m<mods.size(); m++) {
const AstList& files = mods[m]->getFiles();
for (unsigned a=0; a<files.size(); a++) {
AST* ast = files[a];
RefFinder finder(*ast, D);
unsigned count = finder.find();
if (count) printf("replaced %d references in %s\n", count, files[i]->getFileName().c_str());
for (unsigned i=0; i<count; i++) {
std::string temp = finder.locs[i].printToString(SM);
printf("loc %d -> %s\n", finder.locs[i].getRawEncoding(), temp.c_str());
PresumedLoc loc = SM.getPresumedLoc(finder.locs[i]);
assert(!loc.isInvalid() && "Invalid location");
printf(" -> %s:%d:%d\n", loc.getFilename(), loc.getLine(), loc.getColumn());
std::pair<FileID, unsigned> Off = SM.getDecomposedExpansionLoc(finder.locs[i]);
printf("-> offset %d\n", Off.second);
rewriter.ReplaceText(finder.locs[i], oldName.size(), newName);
}
}
}
// Step 3: reparse and check
// TODO
// print output
for (unsigned m=0; m<mods.size(); m++) {
const AstList& files = mods[m]->getFiles();
for (unsigned a=0; a<files.size(); a++) {
AST* ast = files[a];
const RewriteBuffer *RewriteBuf =
rewriter.getRewriteBufferFor(ast->fileID);
if (RewriteBuf) {
printf("====== %s ======\n", ast->getFileName().c_str());
llvm::outs() << std::string(RewriteBuf->begin(), RewriteBuf->end());
}
}
}
// also works!
//bool err = rewriter.overwriteChangedFiles();
//printf("errors = %d\n", err);
#endif
}
示例6: ExpandBuiltinMacro
/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
/// as a builtin macro, handle it and return the next token as 'Tok'.
void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
// Figure out which token this is.
IdentifierInfo *II = Tok.getIdentifierInfo();
assert(II && "Can't be a macro without id info!");
++NumBuiltinMacroExpanded;
SmallString<128> TmpBuffer;
llvm::raw_svector_ostream OS(TmpBuffer);
// Set up the return result.
Tok.setIdentifierInfo(nullptr);
Tok.clearFlag(Token::NeedsCleaning);
if (II == Ident__LINE__) {
// C99 6.10.8: "__LINE__: The presumed line number (within the current
// source file) of the current source line (an integer constant)". This can
// be affected by #line.
SourceLocation Loc = Tok.getLocation();
// Advance to the location of the first _, this might not be the first byte
// of the token if it starts with an escaped newline.
Loc = AdvanceToTokenCharacter(Loc, 0);
// One wrinkle here is that GCC expands __LINE__ to location of the *end* of
// a macro expansion. This doesn't matter for object-like macros, but
// can matter for a function-like macro that expands to contain __LINE__.
// Skip down through expansion points until we find a file loc for the
// end of the expansion history.
Loc = SourceMgr.getExpansionRange(Loc).getEnd();
PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc);
// __LINE__ expands to a simple numeric value.
OS << (PLoc.isValid()? PLoc.getLine() : 1);
Tok.setKind(tok::numeric_constant);
} else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
// C99 6.10.8: "__FILE__: The presumed name of the current source file (a
// character string literal)". This can be affected by #line.
PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
// __BASE_FILE__ is a GNU extension that returns the top of the presumed
// #include stack instead of the current file.
if (II == Ident__BASE_FILE__ && PLoc.isValid()) {
SourceLocation NextLoc = PLoc.getIncludeLoc();
while (NextLoc.isValid()) {
PLoc = SourceMgr.getPresumedLoc(NextLoc);
if (PLoc.isInvalid())
break;
NextLoc = PLoc.getIncludeLoc();
}
}
// Escape this filename. Turn '\' -> '\\' '"' -> '\"'
SmallString<128> FN;
if (PLoc.isValid()) {
FN += PLoc.getFilename();
Lexer::Stringify(FN);
OS << '"' << FN << '"';
}
Tok.setKind(tok::string_literal);
} else if (II == Ident__DATE__) {
Diag(Tok.getLocation(), diag::warn_pp_date_time);
if (!DATELoc.isValid())
ComputeDATE_TIME(DATELoc, TIMELoc, *this);
Tok.setKind(tok::string_literal);
Tok.setLength(strlen("\"Mmm dd yyyy\""));
Tok.setLocation(SourceMgr.createExpansionLoc(DATELoc, Tok.getLocation(),
Tok.getLocation(),
Tok.getLength()));
return;
} else if (II == Ident__TIME__) {
Diag(Tok.getLocation(), diag::warn_pp_date_time);
if (!TIMELoc.isValid())
ComputeDATE_TIME(DATELoc, TIMELoc, *this);
Tok.setKind(tok::string_literal);
Tok.setLength(strlen("\"hh:mm:ss\""));
Tok.setLocation(SourceMgr.createExpansionLoc(TIMELoc, Tok.getLocation(),
Tok.getLocation(),
Tok.getLength()));
return;
} else if (II == Ident__INCLUDE_LEVEL__) {
// Compute the presumed include depth of this token. This can be affected
// by GNU line markers.
unsigned Depth = 0;
PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
if (PLoc.isValid()) {
PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
for (; PLoc.isValid(); ++Depth)
PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
}
// __INCLUDE_LEVEL__ expands to a simple numeric value.
OS << Depth;
Tok.setKind(tok::numeric_constant);
} else if (II == Ident__TIMESTAMP__) {
//.........这里部分代码省略.........