本文整理汇总了C++中SourceManager::getDiagnostics方法的典型用法代码示例。如果您正苦于以下问题:C++ SourceManager::getDiagnostics方法的具体用法?C++ SourceManager::getDiagnostics怎么用?C++ SourceManager::getDiagnostics使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SourceManager
的用法示例。
在下文中一共展示了SourceManager::getDiagnostics方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: findDirectives
/// \brief Lex the specified source file to determine whether it contains
/// any expected-* directives. As a Lexer is used rather than a full-blown
/// Preprocessor, directives inside skipped #if blocks will still be found.
///
/// \return true if any directives were found.
static bool findDirectives(SourceManager &SM, FileID FID,
const LangOptions &LangOpts) {
// Create a raw lexer to pull all the comments out of FID.
if (FID.isInvalid())
return false;
// Create a lexer to lex all the tokens of the main file in raw mode.
const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
Lexer RawLex(FID, FromFile, SM, LangOpts);
// Return comments as tokens, this is how we find expected diagnostics.
RawLex.SetCommentRetentionState(true);
Token Tok;
Tok.setKind(tok::comment);
VerifyDiagnosticConsumer::DirectiveStatus Status =
VerifyDiagnosticConsumer::HasNoDirectives;
while (Tok.isNot(tok::eof)) {
RawLex.Lex(Tok);
if (!Tok.is(tok::comment)) continue;
std::string Comment = RawLex.getSpelling(Tok, SM, LangOpts);
if (Comment.empty()) continue;
// Find first directive.
if (ParseDirective(Comment, 0, SM, Tok.getLocation(),
SM.getDiagnostics(), Status))
return true;
}
return false;
}
示例2: ParseDirective
/// ParseDirective - Go through the comment and see if it indicates expected
/// diagnostics. If so, then put them in the appropriate directive list.
///
/// Returns true if any valid directives were found.
static bool ParseDirective(StringRef S, ExpectedData *ED, SourceManager &SM,
Preprocessor *PP, SourceLocation Pos,
VerifyDiagnosticConsumer::DirectiveStatus &Status,
VerifyDiagnosticConsumer::MarkerTracker &Markers) {
DiagnosticsEngine &Diags = PP ? PP->getDiagnostics() : SM.getDiagnostics();
// First, scan the comment looking for markers.
for (ParseHelper PH(S); !PH.Done();) {
if (!PH.Search("#", true))
break;
PH.C = PH.P;
if (!PH.NextMarker()) {
PH.Next("#");
PH.Advance();
continue;
}
PH.Advance();
Markers.addMarker(PH.Match(), Pos);
}
// A single comment may contain multiple directives.
bool FoundDirective = false;
for (ParseHelper PH(S); !PH.Done();) {
// Search for the initial directive token.
// If one prefix, save time by searching only for its directives.
// Otherwise, search for any potential directive token and check it later.
const auto &Prefixes = Diags.getDiagnosticOptions().VerifyPrefixes;
if (!(Prefixes.size() == 1 ? PH.Search(*Prefixes.begin(), true, true)
: PH.Search("", true, true)))
break;
StringRef DToken = PH.Match();
PH.Advance();
// Default directive kind.
UnattachedDirective D;
const char *KindStr = "string";
// Parse the initial directive token in reverse so we can easily determine
// its exact actual prefix. If we were to parse it from the front instead,
// it would be harder to determine where the prefix ends because there
// might be multiple matching -verify prefixes because some might prefix
// others.
// Regex in initial directive token: -re
if (DToken.endswith("-re")) {
D.RegexKind = true;
KindStr = "regex";
DToken = DToken.substr(0, DToken.size()-3);
}
// Type in initial directive token: -{error|warning|note|no-diagnostics}
bool NoDiag = false;
StringRef DType;
if (DToken.endswith(DType="-error"))
D.DL = ED ? &ED->Errors : nullptr;
else if (DToken.endswith(DType="-warning"))
D.DL = ED ? &ED->Warnings : nullptr;
else if (DToken.endswith(DType="-remark"))
D.DL = ED ? &ED->Remarks : nullptr;
else if (DToken.endswith(DType="-note"))
D.DL = ED ? &ED->Notes : nullptr;
else if (DToken.endswith(DType="-no-diagnostics")) {
NoDiag = true;
if (D.RegexKind)
continue;
}
else
continue;
DToken = DToken.substr(0, DToken.size()-DType.size());
// What's left in DToken is the actual prefix. That might not be a -verify
// prefix even if there is only one -verify prefix (for example, the full
// DToken is foo-bar-warning, but foo is the only -verify prefix).
if (!std::binary_search(Prefixes.begin(), Prefixes.end(), DToken))
continue;
if (NoDiag) {
if (Status == VerifyDiagnosticConsumer::HasOtherExpectedDirectives)
Diags.Report(Pos, diag::err_verify_invalid_no_diags)
<< /*IsExpectedNoDiagnostics=*/true;
else
Status = VerifyDiagnosticConsumer::HasExpectedNoDiagnostics;
continue;
}
if (Status == VerifyDiagnosticConsumer::HasExpectedNoDiagnostics) {
Diags.Report(Pos, diag::err_verify_invalid_no_diags)
<< /*IsExpectedNoDiagnostics=*/false;
continue;
}
Status = VerifyDiagnosticConsumer::HasOtherExpectedDirectives;
// If a directive has been found but we're not interested
// in storing the directive information, return now.
if (!D.DL)
return true;
//.........这里部分代码省略.........
示例3: ParseDirective
/// ParseDirective - Go through the comment and see if it indicates expected
/// diagnostics. If so, then put them in the appropriate directive list.
///
/// Returns true if any valid directives were found.
static bool ParseDirective(StringRef S, ExpectedData *ED, SourceManager &SM,
Preprocessor *PP, SourceLocation Pos,
VerifyDiagnosticConsumer::DirectiveStatus &Status) {
DiagnosticsEngine &Diags = PP ? PP->getDiagnostics() : SM.getDiagnostics();
// A single comment may contain multiple directives.
bool FoundDirective = false;
for (ParseHelper PH(S); !PH.Done();) {
// Search for token: expected
if (!PH.Search("expected", true))
break;
PH.Advance();
// Next token: -
if (!PH.Next("-"))
continue;
PH.Advance();
// Next token: { error | warning | note }
DirectiveList *DL = nullptr;
if (PH.Next("error"))
DL = ED ? &ED->Errors : nullptr;
else if (PH.Next("warning"))
DL = ED ? &ED->Warnings : nullptr;
else if (PH.Next("remark"))
DL = ED ? &ED->Remarks : nullptr;
else if (PH.Next("note"))
DL = ED ? &ED->Notes : nullptr;
else if (PH.Next("no-diagnostics")) {
if (Status == VerifyDiagnosticConsumer::HasOtherExpectedDirectives)
Diags.Report(Pos, diag::err_verify_invalid_no_diags)
<< /*IsExpectedNoDiagnostics=*/true;
else
Status = VerifyDiagnosticConsumer::HasExpectedNoDiagnostics;
continue;
} else
continue;
PH.Advance();
if (Status == VerifyDiagnosticConsumer::HasExpectedNoDiagnostics) {
Diags.Report(Pos, diag::err_verify_invalid_no_diags)
<< /*IsExpectedNoDiagnostics=*/false;
continue;
}
Status = VerifyDiagnosticConsumer::HasOtherExpectedDirectives;
// If a directive has been found but we're not interested
// in storing the directive information, return now.
if (!DL)
return true;
// Default directive kind.
bool RegexKind = false;
const char* KindStr = "string";
// Next optional token: -
if (PH.Next("-re")) {
PH.Advance();
RegexKind = true;
KindStr = "regex";
}
// Next optional token: @
SourceLocation ExpectedLoc;
bool MatchAnyLine = false;
if (!PH.Next("@")) {
ExpectedLoc = Pos;
} else {
PH.Advance();
unsigned Line = 0;
bool FoundPlus = PH.Next("+");
if (FoundPlus || PH.Next("-")) {
// Relative to current line.
PH.Advance();
bool Invalid = false;
unsigned ExpectedLine = SM.getSpellingLineNumber(Pos, &Invalid);
if (!Invalid && PH.Next(Line) && (FoundPlus || Line < ExpectedLine)) {
if (FoundPlus) ExpectedLine += Line;
else ExpectedLine -= Line;
ExpectedLoc = SM.translateLineCol(SM.getFileID(Pos), ExpectedLine, 1);
}
} else if (PH.Next(Line)) {
// Absolute line number.
if (Line > 0)
ExpectedLoc = SM.translateLineCol(SM.getFileID(Pos), Line, 1);
} else if (PP && PH.Search(":")) {
// Specific source file.
StringRef Filename(PH.C, PH.P-PH.C);
PH.Advance();
// Lookup file via Preprocessor, like a #include.
const DirectoryLookup *CurDir;
const FileEntry *FE =
PP->LookupFile(Pos, Filename, false, nullptr, nullptr, CurDir,
nullptr, nullptr, nullptr);
if (!FE) {
//.........这里部分代码省略.........