2011-09-26 08:38:03 +08:00
|
|
|
//===---- VerifyDiagnosticConsumer.cpp - Verifying Diagnostic Client ------===//
|
2009-11-14 11:23:19 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This is a concrete diagnostic client, which buffers the diagnostic messages.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-09-26 08:38:03 +08:00
|
|
|
#include "clang/Frontend/VerifyDiagnosticConsumer.h"
|
2009-11-14 11:23:19 +08:00
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
|
|
|
#include "clang/Frontend/TextDiagnosticBuffer.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
2010-04-29 04:02:30 +08:00
|
|
|
#include "llvm/Support/Regex.h"
|
2009-11-14 11:23:19 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2011-12-15 12:24:37 +08:00
|
|
|
#include <climits>
|
2011-12-15 10:58:00 +08:00
|
|
|
|
2009-11-14 11:23:19 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2011-09-26 08:38:03 +08:00
|
|
|
VerifyDiagnosticConsumer::VerifyDiagnosticConsumer(DiagnosticsEngine &_Diags)
|
2011-09-13 09:26:44 +08:00
|
|
|
: Diags(_Diags), PrimaryClient(Diags.getClient()),
|
|
|
|
OwnsPrimaryClient(Diags.ownsClient()),
|
|
|
|
Buffer(new TextDiagnosticBuffer()), CurrentPreprocessor(0)
|
|
|
|
{
|
|
|
|
Diags.takeClient();
|
2009-11-14 11:23:19 +08:00
|
|
|
}
|
|
|
|
|
2011-09-26 08:38:03 +08:00
|
|
|
VerifyDiagnosticConsumer::~VerifyDiagnosticConsumer() {
|
2011-09-13 09:26:44 +08:00
|
|
|
CheckDiagnostics();
|
|
|
|
Diags.takeClient();
|
|
|
|
if (OwnsPrimaryClient)
|
|
|
|
delete PrimaryClient;
|
2009-11-14 11:23:19 +08:00
|
|
|
}
|
|
|
|
|
2011-09-26 07:39:51 +08:00
|
|
|
// DiagnosticConsumer interface.
|
2009-11-14 11:23:19 +08:00
|
|
|
|
2011-09-26 08:38:03 +08:00
|
|
|
void VerifyDiagnosticConsumer::BeginSourceFile(const LangOptions &LangOpts,
|
2012-01-21 00:28:04 +08:00
|
|
|
const Preprocessor *PP) {
|
2009-11-14 11:23:19 +08:00
|
|
|
// FIXME: Const hack, we screw up the preprocessor but in practice its ok
|
|
|
|
// because it doesn't get reused. It would be better if we could make a copy
|
|
|
|
// though.
|
|
|
|
CurrentPreprocessor = const_cast<Preprocessor*>(PP);
|
|
|
|
|
|
|
|
PrimaryClient->BeginSourceFile(LangOpts, PP);
|
|
|
|
}
|
|
|
|
|
2011-09-26 08:38:03 +08:00
|
|
|
void VerifyDiagnosticConsumer::EndSourceFile() {
|
2009-11-14 11:23:19 +08:00
|
|
|
CheckDiagnostics();
|
|
|
|
|
|
|
|
PrimaryClient->EndSourceFile();
|
|
|
|
|
|
|
|
CurrentPreprocessor = 0;
|
|
|
|
}
|
2009-11-14 15:53:24 +08:00
|
|
|
|
2011-09-26 08:38:03 +08:00
|
|
|
void VerifyDiagnosticConsumer::HandleDiagnostic(
|
2011-09-26 09:18:08 +08:00
|
|
|
DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) {
|
2011-07-26 03:18:12 +08:00
|
|
|
if (FirstErrorFID.isInvalid() && Info.hasSourceManager()) {
|
|
|
|
const SourceManager &SM = Info.getSourceManager();
|
|
|
|
FirstErrorFID = SM.getFileID(Info.getLocation());
|
|
|
|
}
|
2009-11-14 11:23:19 +08:00
|
|
|
// Send the diagnostic to the buffer, we will check it once we reach the end
|
|
|
|
// of the source file (or are destructed).
|
|
|
|
Buffer->HandleDiagnostic(DiagLevel, Info);
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Checking diagnostics implementation.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
typedef TextDiagnosticBuffer::DiagList DiagList;
|
|
|
|
typedef TextDiagnosticBuffer::const_iterator const_diag_iterator;
|
|
|
|
|
2010-04-29 04:02:30 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// Directive - Abstract class representing a parsed verify directive.
|
2009-11-14 11:23:19 +08:00
|
|
|
///
|
2010-04-29 04:02:30 +08:00
|
|
|
class Directive {
|
|
|
|
public:
|
|
|
|
static Directive* Create(bool RegexKind, const SourceLocation &Location,
|
|
|
|
const std::string &Text, unsigned Count);
|
|
|
|
public:
|
2011-12-15 10:28:16 +08:00
|
|
|
/// Constant representing one or more matches aka regex "+".
|
|
|
|
static const unsigned OneOrMoreCount = UINT_MAX;
|
|
|
|
|
2010-04-29 04:02:30 +08:00
|
|
|
SourceLocation Location;
|
|
|
|
const std::string Text;
|
|
|
|
unsigned Count;
|
|
|
|
|
|
|
|
virtual ~Directive() { }
|
|
|
|
|
|
|
|
// Returns true if directive text is valid.
|
|
|
|
// Otherwise returns false and populates E.
|
|
|
|
virtual bool isValid(std::string &Error) = 0;
|
|
|
|
|
|
|
|
// Returns true on match.
|
|
|
|
virtual bool Match(const std::string &S) = 0;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
Directive(const SourceLocation &Location, const std::string &Text,
|
|
|
|
unsigned Count)
|
|
|
|
: Location(Location), Text(Text), Count(Count) { }
|
|
|
|
|
|
|
|
private:
|
2010-08-15 18:17:39 +08:00
|
|
|
Directive(const Directive&); // DO NOT IMPLEMENT
|
|
|
|
void operator=(const Directive&); // DO NOT IMPLEMENT
|
2010-04-29 04:02:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// StandardDirective - Directive with string matching.
|
|
|
|
///
|
|
|
|
class StandardDirective : public Directive {
|
|
|
|
public:
|
|
|
|
StandardDirective(const SourceLocation &Location, const std::string &Text,
|
|
|
|
unsigned Count)
|
|
|
|
: Directive(Location, Text, Count) { }
|
|
|
|
|
|
|
|
virtual bool isValid(std::string &Error) {
|
|
|
|
// all strings are considered valid; even empty ones
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool Match(const std::string &S) {
|
2011-12-15 08:38:15 +08:00
|
|
|
return S.find(Text) != std::string::npos;
|
2010-04-29 04:02:30 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// RegexDirective - Directive with regular-expression matching.
|
|
|
|
///
|
|
|
|
class RegexDirective : public Directive {
|
|
|
|
public:
|
|
|
|
RegexDirective(const SourceLocation &Location, const std::string &Text,
|
|
|
|
unsigned Count)
|
|
|
|
: Directive(Location, Text, Count), Regex(Text) { }
|
|
|
|
|
|
|
|
virtual bool isValid(std::string &Error) {
|
|
|
|
if (Regex.isValid(Error))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool Match(const std::string &S) {
|
|
|
|
return Regex.match(S);
|
|
|
|
}
|
2009-11-14 11:23:19 +08:00
|
|
|
|
2010-04-29 04:02:30 +08:00
|
|
|
private:
|
|
|
|
llvm::Regex Regex;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::vector<Directive*> DirectiveList;
|
|
|
|
|
|
|
|
/// ExpectedData - owns directive objects and deletes on destructor.
|
|
|
|
///
|
|
|
|
struct ExpectedData {
|
|
|
|
DirectiveList Errors;
|
|
|
|
DirectiveList Warnings;
|
|
|
|
DirectiveList Notes;
|
|
|
|
|
|
|
|
~ExpectedData() {
|
|
|
|
DirectiveList* Lists[] = { &Errors, &Warnings, &Notes, 0 };
|
|
|
|
for (DirectiveList **PL = Lists; *PL; ++PL) {
|
|
|
|
DirectiveList * const L = *PL;
|
|
|
|
for (DirectiveList::iterator I = L->begin(), E = L->end(); I != E; ++I)
|
|
|
|
delete *I;
|
2009-11-14 11:23:19 +08:00
|
|
|
}
|
2010-04-29 04:02:30 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ParseHelper
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ParseHelper(const char *Begin, const char *End)
|
|
|
|
: Begin(Begin), End(End), C(Begin), P(Begin), PEnd(NULL) { }
|
|
|
|
|
|
|
|
// Return true if string literal is next.
|
2011-07-23 18:55:15 +08:00
|
|
|
bool Next(StringRef S) {
|
2010-04-29 04:02:30 +08:00
|
|
|
P = C;
|
2010-09-02 01:28:48 +08:00
|
|
|
PEnd = C + S.size();
|
2010-04-29 04:02:30 +08:00
|
|
|
if (PEnd > End)
|
|
|
|
return false;
|
2010-09-02 01:28:48 +08:00
|
|
|
return !memcmp(P, S.data(), S.size());
|
2010-04-29 04:02:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return true if number is next.
|
|
|
|
// Output N only if number is next.
|
|
|
|
bool Next(unsigned &N) {
|
|
|
|
unsigned TMP = 0;
|
|
|
|
P = C;
|
|
|
|
for (; P < End && P[0] >= '0' && P[0] <= '9'; ++P) {
|
|
|
|
TMP *= 10;
|
|
|
|
TMP += P[0] - '0';
|
2009-11-14 11:23:19 +08:00
|
|
|
}
|
2010-04-29 04:02:30 +08:00
|
|
|
if (P == C)
|
|
|
|
return false;
|
|
|
|
PEnd = P;
|
|
|
|
N = TMP;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return true if string literal is found.
|
|
|
|
// When true, P marks begin-position of S in content.
|
2011-07-23 18:55:15 +08:00
|
|
|
bool Search(StringRef S) {
|
2010-04-29 04:02:30 +08:00
|
|
|
P = std::search(C, End, S.begin(), S.end());
|
2010-09-02 01:28:48 +08:00
|
|
|
PEnd = P + S.size();
|
2010-04-29 04:02:30 +08:00
|
|
|
return P != End;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advance 1-past previous next/search.
|
|
|
|
// Behavior is undefined if previous next/search failed.
|
|
|
|
bool Advance() {
|
|
|
|
C = PEnd;
|
|
|
|
return C < End;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip zero or more whitespace.
|
|
|
|
void SkipWhitespace() {
|
|
|
|
for (; C < End && isspace(*C); ++C)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return true if EOF reached.
|
|
|
|
bool Done() {
|
|
|
|
return !(C < End);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * const Begin; // beginning of expected content
|
|
|
|
const char * const End; // end of expected content (1-past)
|
|
|
|
const char *C; // position of next char in content
|
|
|
|
const char *P;
|
|
|
|
|
|
|
|
private:
|
|
|
|
const char *PEnd; // previous next/search subject end (1-past)
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace anonymous
|
|
|
|
|
|
|
|
/// ParseDirective - Go through the comment and see if it indicates expected
|
|
|
|
/// diagnostics. If so, then put them in the appropriate directive list.
|
|
|
|
///
|
|
|
|
static void ParseDirective(const char *CommentStart, unsigned CommentLen,
|
|
|
|
ExpectedData &ED, Preprocessor &PP,
|
|
|
|
SourceLocation Pos) {
|
|
|
|
// A single comment may contain multiple directives.
|
|
|
|
for (ParseHelper PH(CommentStart, CommentStart+CommentLen); !PH.Done();) {
|
|
|
|
// search for token: expected
|
|
|
|
if (!PH.Search("expected"))
|
|
|
|
break;
|
|
|
|
PH.Advance();
|
|
|
|
|
|
|
|
// next token: -
|
|
|
|
if (!PH.Next("-"))
|
|
|
|
continue;
|
|
|
|
PH.Advance();
|
|
|
|
|
|
|
|
// next token: { error | warning | note }
|
|
|
|
DirectiveList* DL = NULL;
|
|
|
|
if (PH.Next("error"))
|
|
|
|
DL = &ED.Errors;
|
|
|
|
else if (PH.Next("warning"))
|
|
|
|
DL = &ED.Warnings;
|
|
|
|
else if (PH.Next("note"))
|
|
|
|
DL = &ED.Notes;
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
PH.Advance();
|
2009-11-14 11:23:19 +08:00
|
|
|
|
2010-04-29 04:02:30 +08:00
|
|
|
// default directive kind
|
|
|
|
bool RegexKind = false;
|
|
|
|
const char* KindStr = "string";
|
2009-11-14 11:23:19 +08:00
|
|
|
|
2010-04-29 04:02:30 +08:00
|
|
|
// next optional token: -
|
|
|
|
if (PH.Next("-re")) {
|
|
|
|
PH.Advance();
|
|
|
|
RegexKind = true;
|
|
|
|
KindStr = "regex";
|
2009-11-14 11:23:19 +08:00
|
|
|
}
|
|
|
|
|
2010-04-29 04:02:30 +08:00
|
|
|
// skip optional whitespace
|
|
|
|
PH.SkipWhitespace();
|
|
|
|
|
2011-12-15 10:28:16 +08:00
|
|
|
// next optional token: positive integer or a '+'.
|
2010-04-29 04:02:30 +08:00
|
|
|
unsigned Count = 1;
|
|
|
|
if (PH.Next(Count))
|
|
|
|
PH.Advance();
|
2011-12-15 10:28:16 +08:00
|
|
|
else if (PH.Next("+")) {
|
|
|
|
Count = Directive::OneOrMoreCount;
|
|
|
|
PH.Advance();
|
|
|
|
}
|
2010-04-29 04:02:30 +08:00
|
|
|
|
|
|
|
// skip optional whitespace
|
|
|
|
PH.SkipWhitespace();
|
|
|
|
|
|
|
|
// next token: {{
|
|
|
|
if (!PH.Next("{{")) {
|
2011-09-20 04:40:19 +08:00
|
|
|
PP.Diag(Pos.getLocWithOffset(PH.C-PH.Begin),
|
2010-04-29 04:02:30 +08:00
|
|
|
diag::err_verify_missing_start) << KindStr;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
PH.Advance();
|
|
|
|
const char* const ContentBegin = PH.C; // mark content begin
|
2009-11-14 11:23:19 +08:00
|
|
|
|
2010-04-29 04:02:30 +08:00
|
|
|
// search for token: }}
|
|
|
|
if (!PH.Search("}}")) {
|
2011-09-20 04:40:19 +08:00
|
|
|
PP.Diag(Pos.getLocWithOffset(PH.C-PH.Begin),
|
2010-04-29 04:02:30 +08:00
|
|
|
diag::err_verify_missing_end) << KindStr;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const char* const ContentEnd = PH.P; // mark content end
|
|
|
|
PH.Advance();
|
|
|
|
|
|
|
|
// build directive text; convert \n to newlines
|
|
|
|
std::string Text;
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef NewlineStr = "\\n";
|
|
|
|
StringRef Content(ContentBegin, ContentEnd-ContentBegin);
|
2010-04-29 04:02:30 +08:00
|
|
|
size_t CPos = 0;
|
|
|
|
size_t FPos;
|
2011-07-23 18:55:15 +08:00
|
|
|
while ((FPos = Content.find(NewlineStr, CPos)) != StringRef::npos) {
|
2010-04-29 04:02:30 +08:00
|
|
|
Text += Content.substr(CPos, FPos-CPos);
|
|
|
|
Text += '\n';
|
|
|
|
CPos = FPos + NewlineStr.size();
|
|
|
|
}
|
|
|
|
if (Text.empty())
|
|
|
|
Text.assign(ContentBegin, ContentEnd);
|
|
|
|
|
|
|
|
// construct new directive
|
|
|
|
Directive *D = Directive::Create(RegexKind, Pos, Text, Count);
|
|
|
|
std::string Error;
|
|
|
|
if (D->isValid(Error))
|
|
|
|
DL->push_back(D);
|
|
|
|
else {
|
2011-09-20 04:40:19 +08:00
|
|
|
PP.Diag(Pos.getLocWithOffset(ContentBegin-PH.Begin),
|
2010-04-29 04:02:30 +08:00
|
|
|
diag::err_verify_invalid_content)
|
|
|
|
<< KindStr << Error;
|
|
|
|
}
|
2009-11-14 11:23:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// FindExpectedDiags - Lex the main source file to find all of the
|
|
|
|
// expected errors and warnings.
|
2011-07-26 03:18:12 +08:00
|
|
|
static void FindExpectedDiags(Preprocessor &PP, ExpectedData &ED, FileID FID) {
|
|
|
|
// Create a raw lexer to pull all the comments out of FID.
|
|
|
|
if (FID.isInvalid())
|
2009-11-14 11:23:19 +08:00
|
|
|
return;
|
|
|
|
|
2011-07-26 03:18:12 +08:00
|
|
|
SourceManager& SM = PP.getSourceManager();
|
2009-11-14 11:23:19 +08:00
|
|
|
// Create a lexer to lex all the tokens of the main file in raw mode.
|
2009-11-30 12:18:44 +08:00
|
|
|
const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
|
2012-03-11 15:00:24 +08:00
|
|
|
Lexer RawLex(FID, FromFile, SM, PP.getLangOpts());
|
2009-11-14 11:23:19 +08:00
|
|
|
|
|
|
|
// Return comments as tokens, this is how we find expected diagnostics.
|
|
|
|
RawLex.SetCommentRetentionState(true);
|
|
|
|
|
|
|
|
Token Tok;
|
|
|
|
Tok.setKind(tok::comment);
|
|
|
|
while (Tok.isNot(tok::eof)) {
|
|
|
|
RawLex.Lex(Tok);
|
|
|
|
if (!Tok.is(tok::comment)) continue;
|
|
|
|
|
|
|
|
std::string Comment = PP.getSpelling(Tok);
|
|
|
|
if (Comment.empty()) continue;
|
|
|
|
|
2010-04-29 04:02:30 +08:00
|
|
|
// Find all expected errors/warnings/notes.
|
|
|
|
ParseDirective(&Comment[0], Comment.size(), ED, PP, Tok.getLocation());
|
2009-11-14 11:23:19 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// PrintProblem - This takes a diagnostic map of the delta between expected and
|
|
|
|
/// seen diagnostics. If there's anything in it, then something unexpected
|
|
|
|
/// happened. Print the map out in a nice format and return "true". If the map
|
|
|
|
/// is empty and we're not going to print things, then return "false".
|
|
|
|
///
|
2011-09-26 07:23:43 +08:00
|
|
|
static unsigned PrintProblem(DiagnosticsEngine &Diags, SourceManager *SourceMgr,
|
2009-11-14 11:23:19 +08:00
|
|
|
const_diag_iterator diag_begin,
|
|
|
|
const_diag_iterator diag_end,
|
|
|
|
const char *Kind, bool Expected) {
|
|
|
|
if (diag_begin == diag_end) return 0;
|
|
|
|
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<256> Fmt;
|
2009-11-14 11:23:19 +08:00
|
|
|
llvm::raw_svector_ostream OS(Fmt);
|
|
|
|
for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I) {
|
2009-11-14 15:53:24 +08:00
|
|
|
if (I->first.isInvalid() || !SourceMgr)
|
2009-11-14 11:23:19 +08:00
|
|
|
OS << "\n (frontend)";
|
|
|
|
else
|
2011-02-23 08:47:48 +08:00
|
|
|
OS << "\n Line " << SourceMgr->getPresumedLineNumber(I->first);
|
2009-11-14 11:23:19 +08:00
|
|
|
OS << ": " << I->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
Diags.Report(diag::err_verify_inconsistent_diags)
|
2009-11-14 15:53:24 +08:00
|
|
|
<< Kind << !Expected << OS.str();
|
2009-11-14 11:23:19 +08:00
|
|
|
return std::distance(diag_begin, diag_end);
|
|
|
|
}
|
|
|
|
|
2011-09-26 07:23:43 +08:00
|
|
|
static unsigned PrintProblem(DiagnosticsEngine &Diags, SourceManager *SourceMgr,
|
2010-04-29 04:02:30 +08:00
|
|
|
DirectiveList &DL, const char *Kind,
|
|
|
|
bool Expected) {
|
|
|
|
if (DL.empty())
|
|
|
|
return 0;
|
|
|
|
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<256> Fmt;
|
2010-04-29 04:02:30 +08:00
|
|
|
llvm::raw_svector_ostream OS(Fmt);
|
|
|
|
for (DirectiveList::iterator I = DL.begin(), E = DL.end(); I != E; ++I) {
|
|
|
|
Directive& D = **I;
|
|
|
|
if (D.Location.isInvalid() || !SourceMgr)
|
|
|
|
OS << "\n (frontend)";
|
|
|
|
else
|
2011-02-23 08:47:48 +08:00
|
|
|
OS << "\n Line " << SourceMgr->getPresumedLineNumber(D.Location);
|
2010-04-29 04:02:30 +08:00
|
|
|
OS << ": " << D.Text;
|
|
|
|
}
|
|
|
|
|
|
|
|
Diags.Report(diag::err_verify_inconsistent_diags)
|
|
|
|
<< Kind << !Expected << OS.str();
|
|
|
|
return DL.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CheckLists - Compare expected to seen diagnostic lists and return the
|
|
|
|
/// the difference between them.
|
2009-11-14 11:23:19 +08:00
|
|
|
///
|
2011-09-26 07:23:43 +08:00
|
|
|
static unsigned CheckLists(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
|
2010-04-29 04:02:30 +08:00
|
|
|
const char *Label,
|
|
|
|
DirectiveList &Left,
|
|
|
|
const_diag_iterator d2_begin,
|
|
|
|
const_diag_iterator d2_end) {
|
|
|
|
DirectiveList LeftOnly;
|
2009-11-14 11:23:19 +08:00
|
|
|
DiagList Right(d2_begin, d2_end);
|
|
|
|
|
2010-04-29 04:02:30 +08:00
|
|
|
for (DirectiveList::iterator I = Left.begin(), E = Left.end(); I != E; ++I) {
|
|
|
|
Directive& D = **I;
|
2011-02-23 08:47:48 +08:00
|
|
|
unsigned LineNo1 = SourceMgr.getPresumedLineNumber(D.Location);
|
2011-12-15 10:28:16 +08:00
|
|
|
bool FoundOnce = false;
|
2009-11-14 11:23:19 +08:00
|
|
|
|
2010-04-29 04:02:30 +08:00
|
|
|
for (unsigned i = 0; i < D.Count; ++i) {
|
|
|
|
DiagList::iterator II, IE;
|
|
|
|
for (II = Right.begin(), IE = Right.end(); II != IE; ++II) {
|
2011-02-23 08:47:48 +08:00
|
|
|
unsigned LineNo2 = SourceMgr.getPresumedLineNumber(II->first);
|
2010-04-29 04:02:30 +08:00
|
|
|
if (LineNo1 != LineNo2)
|
|
|
|
continue;
|
2009-11-14 11:23:19 +08:00
|
|
|
|
2010-04-29 04:02:30 +08:00
|
|
|
const std::string &RightText = II->second;
|
|
|
|
if (D.Match(RightText))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (II == IE) {
|
2011-12-17 02:28:45 +08:00
|
|
|
if (D.Count == D.OneOrMoreCount) {
|
|
|
|
if (!FoundOnce)
|
|
|
|
LeftOnly.push_back(*I);
|
|
|
|
// We are only interested in at least one match, so exit the loop.
|
2011-12-15 10:28:16 +08:00
|
|
|
break;
|
|
|
|
}
|
2010-04-29 04:02:30 +08:00
|
|
|
// Not found.
|
|
|
|
LeftOnly.push_back(*I);
|
|
|
|
} else {
|
|
|
|
// Found. The same cannot be found twice.
|
|
|
|
Right.erase(II);
|
2011-12-15 10:28:16 +08:00
|
|
|
FoundOnce = true;
|
2009-11-14 11:23:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Now all that's left in Right are those that were not matched.
|
2011-12-17 21:00:31 +08:00
|
|
|
unsigned num = PrintProblem(Diags, &SourceMgr, LeftOnly, Label, true);
|
|
|
|
num += PrintProblem(Diags, &SourceMgr, Right.begin(), Right.end(),
|
|
|
|
Label, false);
|
|
|
|
return num;
|
2009-11-14 11:23:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// CheckResults - This compares the expected results to those that
|
|
|
|
/// were actually reported. It emits any discrepencies. Return "true" if there
|
|
|
|
/// were problems. Return "false" otherwise.
|
|
|
|
///
|
2011-09-26 07:23:43 +08:00
|
|
|
static unsigned CheckResults(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
|
2009-11-14 11:23:19 +08:00
|
|
|
const TextDiagnosticBuffer &Buffer,
|
2010-04-29 04:02:30 +08:00
|
|
|
ExpectedData &ED) {
|
2009-11-14 11:23:19 +08:00
|
|
|
// We want to capture the delta between what was expected and what was
|
|
|
|
// seen.
|
|
|
|
//
|
|
|
|
// Expected \ Seen - set expected but not seen
|
|
|
|
// Seen \ Expected - set seen but not expected
|
|
|
|
unsigned NumProblems = 0;
|
|
|
|
|
|
|
|
// See if there are error mismatches.
|
2010-04-29 04:02:30 +08:00
|
|
|
NumProblems += CheckLists(Diags, SourceMgr, "error", ED.Errors,
|
|
|
|
Buffer.err_begin(), Buffer.err_end());
|
2009-11-14 15:53:24 +08:00
|
|
|
|
2009-11-14 11:23:19 +08:00
|
|
|
// See if there are warning mismatches.
|
2010-04-29 04:02:30 +08:00
|
|
|
NumProblems += CheckLists(Diags, SourceMgr, "warning", ED.Warnings,
|
|
|
|
Buffer.warn_begin(), Buffer.warn_end());
|
2009-11-14 11:23:19 +08:00
|
|
|
|
|
|
|
// See if there are note mismatches.
|
2010-04-29 04:02:30 +08:00
|
|
|
NumProblems += CheckLists(Diags, SourceMgr, "note", ED.Notes,
|
|
|
|
Buffer.note_begin(), Buffer.note_end());
|
2009-11-14 11:23:19 +08:00
|
|
|
|
|
|
|
return NumProblems;
|
|
|
|
}
|
|
|
|
|
2011-09-26 08:38:03 +08:00
|
|
|
void VerifyDiagnosticConsumer::CheckDiagnostics() {
|
2010-04-29 04:02:30 +08:00
|
|
|
ExpectedData ED;
|
2009-11-14 11:23:19 +08:00
|
|
|
|
|
|
|
// Ensure any diagnostics go to the primary client.
|
2011-09-13 09:26:44 +08:00
|
|
|
bool OwnsCurClient = Diags.ownsClient();
|
2011-09-26 07:39:51 +08:00
|
|
|
DiagnosticConsumer *CurClient = Diags.takeClient();
|
2011-09-13 09:26:44 +08:00
|
|
|
Diags.setClient(PrimaryClient, false);
|
2009-11-14 11:23:19 +08:00
|
|
|
|
|
|
|
// If we have a preprocessor, scan the source for expected diagnostic
|
|
|
|
// markers. If not then any diagnostics are unexpected.
|
|
|
|
if (CurrentPreprocessor) {
|
2011-07-26 03:18:12 +08:00
|
|
|
SourceManager &SM = CurrentPreprocessor->getSourceManager();
|
|
|
|
// Extract expected-error strings from main file.
|
|
|
|
FindExpectedDiags(*CurrentPreprocessor, ED, SM.getMainFileID());
|
|
|
|
// Only check for expectations in other diagnostic locations
|
|
|
|
// if they are not the main file (via ID or FileEntry) - the main
|
|
|
|
// file has already been looked at, and its expectations must not
|
|
|
|
// be added twice.
|
|
|
|
if (!FirstErrorFID.isInvalid() && FirstErrorFID != SM.getMainFileID()
|
|
|
|
&& (!SM.getFileEntryForID(FirstErrorFID)
|
|
|
|
|| (SM.getFileEntryForID(FirstErrorFID) !=
|
2011-08-24 21:36:19 +08:00
|
|
|
SM.getFileEntryForID(SM.getMainFileID())))) {
|
2011-07-26 03:18:12 +08:00
|
|
|
FindExpectedDiags(*CurrentPreprocessor, ED, FirstErrorFID);
|
2011-08-24 21:36:19 +08:00
|
|
|
FirstErrorFID = FileID();
|
|
|
|
}
|
2009-11-14 11:23:19 +08:00
|
|
|
|
2009-11-14 15:53:24 +08:00
|
|
|
// Check that the expected diagnostics occurred.
|
2011-07-26 03:18:12 +08:00
|
|
|
NumErrors += CheckResults(Diags, SM, *Buffer, ED);
|
2009-11-14 15:53:24 +08:00
|
|
|
} else {
|
|
|
|
NumErrors += (PrintProblem(Diags, 0,
|
|
|
|
Buffer->err_begin(), Buffer->err_end(),
|
|
|
|
"error", false) +
|
|
|
|
PrintProblem(Diags, 0,
|
|
|
|
Buffer->warn_begin(), Buffer->warn_end(),
|
|
|
|
"warn", false) +
|
|
|
|
PrintProblem(Diags, 0,
|
|
|
|
Buffer->note_begin(), Buffer->note_end(),
|
|
|
|
"note", false));
|
|
|
|
}
|
2009-11-14 11:23:19 +08:00
|
|
|
|
2010-08-19 06:29:43 +08:00
|
|
|
Diags.takeClient();
|
2011-09-13 09:26:44 +08:00
|
|
|
Diags.setClient(CurClient, OwnsCurClient);
|
2009-11-14 11:23:19 +08:00
|
|
|
|
|
|
|
// Reset the buffer, we have processed all the diagnostics in it.
|
|
|
|
Buffer.reset(new TextDiagnosticBuffer());
|
|
|
|
}
|
2010-04-29 04:02:30 +08:00
|
|
|
|
2011-09-29 08:38:00 +08:00
|
|
|
DiagnosticConsumer *
|
|
|
|
VerifyDiagnosticConsumer::clone(DiagnosticsEngine &Diags) const {
|
|
|
|
if (!Diags.getClient())
|
|
|
|
Diags.setClient(PrimaryClient->clone(Diags));
|
|
|
|
|
|
|
|
return new VerifyDiagnosticConsumer(Diags);
|
|
|
|
}
|
|
|
|
|
2010-04-29 04:02:30 +08:00
|
|
|
Directive* Directive::Create(bool RegexKind, const SourceLocation &Location,
|
|
|
|
const std::string &Text, unsigned Count) {
|
|
|
|
if (RegexKind)
|
|
|
|
return new RegexDirective(Location, Text, Count);
|
|
|
|
return new StandardDirective(Location, Text, Count);
|
|
|
|
}
|