2007-06-28 01:24:55 +08:00
|
|
|
//===--- DiagChecker.cpp - Diagnostic Checking Functions ------------------===//
|
2007-06-27 11:19:45 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-06-27 11:19:45 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Process the input files and check that the diagnostic messages are expected.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-05-19 12:14:29 +08:00
|
|
|
#include "clang/Frontend/Utils.h"
|
2009-03-02 14:16:29 +08:00
|
|
|
#include "clang/Frontend/TextDiagnosticBuffer.h"
|
2008-02-06 08:23:21 +08:00
|
|
|
#include "clang/Sema/ParseAST.h"
|
2007-09-16 06:56:56 +08:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2007-06-27 11:19:45 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2009-12-03 17:14:02 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2007-06-27 11:19:45 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2007-06-27 15:24:11 +08:00
|
|
|
typedef TextDiagnosticBuffer::DiagList DiagList;
|
2007-06-28 13:17:33 +08:00
|
|
|
typedef TextDiagnosticBuffer::const_iterator const_diag_iterator;
|
2007-06-27 15:24:11 +08:00
|
|
|
|
2008-11-24 07:38:26 +08:00
|
|
|
static void EmitError(Preprocessor &PP, SourceLocation Pos, const char *String){
|
|
|
|
unsigned ID = PP.getDiagnostics().getCustomDiagID(Diagnostic::Error, String);
|
|
|
|
PP.Diag(Pos, ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-27 11:19:45 +08:00
|
|
|
// USING THE DIAGNOSTIC CHECKER:
|
|
|
|
//
|
|
|
|
// Indicating that a line expects an error or a warning is simple. Put a comment
|
|
|
|
// on the line that has the diagnostic, use "expected-{error,warning}" to tag
|
|
|
|
// if it's an expected error or warning, and place the expected text between {{
|
|
|
|
// and }} markers. The full text doesn't have to be included, only enough to
|
|
|
|
// ensure that the correct diagnostic was emitted.
|
|
|
|
//
|
|
|
|
// Here's an example:
|
|
|
|
//
|
|
|
|
// int A = B; // expected-error {{use of undeclared identifier 'B'}}
|
|
|
|
//
|
|
|
|
// You can place as many diagnostics on one line as you wish. To make the code
|
|
|
|
// more readable, you can use slash-newline to separate out the diagnostics.
|
2009-02-08 03:52:04 +08:00
|
|
|
//
|
|
|
|
// The simple syntax above allows each specification to match exactly one error.
|
|
|
|
// You can use the extended syntax to customize this. The extended syntax is
|
|
|
|
// "expected-<type> <n> {{diag text}}", where <type> is one of "error",
|
|
|
|
// "warning" or "note", and <n> is a positive integer. This allows the
|
|
|
|
// diagnostic to appear as many times as specified. Example:
|
|
|
|
//
|
|
|
|
// void f(); // expected-note 2 {{previous declaration is here}}
|
|
|
|
//
|
2007-06-27 11:19:45 +08:00
|
|
|
|
|
|
|
/// FindDiagnostics - Go through the comment and see if it indicates expected
|
|
|
|
/// diagnostics. If so, then put them in a diagnostic list.
|
2009-09-09 23:08:12 +08:00
|
|
|
///
|
2008-11-24 09:28:17 +08:00
|
|
|
static void FindDiagnostics(const char *CommentStart, unsigned CommentLen,
|
2007-06-27 11:19:45 +08:00
|
|
|
DiagList &ExpectedDiags,
|
2008-11-24 09:28:17 +08:00
|
|
|
Preprocessor &PP, SourceLocation Pos,
|
|
|
|
const char *ExpectedStr) {
|
|
|
|
const char *CommentEnd = CommentStart+CommentLen;
|
|
|
|
unsigned ExpectedStrLen = strlen(ExpectedStr);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-24 09:28:17 +08:00
|
|
|
// Find all expected-foo diagnostics in the string and add them to
|
|
|
|
// ExpectedDiags.
|
|
|
|
while (CommentStart != CommentEnd) {
|
|
|
|
CommentStart = std::find(CommentStart, CommentEnd, 'e');
|
|
|
|
if (unsigned(CommentEnd-CommentStart) < ExpectedStrLen) return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-24 09:28:17 +08:00
|
|
|
// If this isn't expected-foo, ignore it.
|
|
|
|
if (memcmp(CommentStart, ExpectedStr, ExpectedStrLen)) {
|
|
|
|
++CommentStart;
|
|
|
|
continue;
|
2007-06-27 11:19:45 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-24 09:28:17 +08:00
|
|
|
CommentStart += ExpectedStrLen;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-24 09:28:17 +08:00
|
|
|
// Skip whitespace.
|
|
|
|
while (CommentStart != CommentEnd &&
|
|
|
|
isspace(CommentStart[0]))
|
|
|
|
++CommentStart;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-08 03:52:04 +08:00
|
|
|
// Default, if we find the '{' now, is 1 time.
|
|
|
|
int Times = 1;
|
|
|
|
int Temp = 0;
|
|
|
|
// In extended syntax, there could be a digit now.
|
|
|
|
while (CommentStart != CommentEnd &&
|
|
|
|
CommentStart[0] >= '0' && CommentStart[0] <= '9') {
|
|
|
|
Temp *= 10;
|
|
|
|
Temp += CommentStart[0] - '0';
|
|
|
|
++CommentStart;
|
|
|
|
}
|
|
|
|
if (Temp > 0)
|
|
|
|
Times = Temp;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-08 03:52:04 +08:00
|
|
|
// Skip whitespace again.
|
|
|
|
while (CommentStart != CommentEnd &&
|
|
|
|
isspace(CommentStart[0]))
|
|
|
|
++CommentStart;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-24 09:28:17 +08:00
|
|
|
// We should have a {{ now.
|
|
|
|
if (CommentEnd-CommentStart < 2 ||
|
|
|
|
CommentStart[0] != '{' || CommentStart[1] != '{') {
|
|
|
|
if (std::find(CommentStart, CommentEnd, '{') != CommentEnd)
|
|
|
|
EmitError(PP, Pos, "bogus characters before '{{' in expected string");
|
|
|
|
else
|
|
|
|
EmitError(PP, Pos, "cannot find start ('{{') of expected string");
|
2008-11-24 07:38:26 +08:00
|
|
|
return;
|
2009-02-08 03:52:04 +08:00
|
|
|
}
|
2008-11-24 09:28:17 +08:00
|
|
|
CommentStart += 2;
|
|
|
|
|
|
|
|
// Find the }}.
|
|
|
|
const char *ExpectedEnd = CommentStart;
|
|
|
|
while (1) {
|
|
|
|
ExpectedEnd = std::find(ExpectedEnd, CommentEnd, '}');
|
|
|
|
if (CommentEnd-ExpectedEnd < 2) {
|
|
|
|
EmitError(PP, Pos, "cannot find end ('}}') of expected string");
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-24 09:28:17 +08:00
|
|
|
if (ExpectedEnd[1] == '}')
|
|
|
|
break;
|
|
|
|
|
|
|
|
++ExpectedEnd; // Skip over singular }'s
|
2007-06-27 11:19:45 +08:00
|
|
|
}
|
|
|
|
|
2008-11-24 09:28:17 +08:00
|
|
|
std::string Msg(CommentStart, ExpectedEnd);
|
|
|
|
std::string::size_type FindPos;
|
2008-11-24 07:38:26 +08:00
|
|
|
while ((FindPos = Msg.find("\\n")) != std::string::npos)
|
2008-10-27 03:05:16 +08:00
|
|
|
Msg.replace(FindPos, 2, "\n");
|
2009-02-08 03:52:04 +08:00
|
|
|
// Add is possibly multiple times.
|
|
|
|
for (int i = 0; i < Times; ++i)
|
|
|
|
ExpectedDiags.push_back(std::make_pair(Pos, Msg));
|
|
|
|
|
2008-11-24 09:28:17 +08:00
|
|
|
CommentStart = ExpectedEnd;
|
2007-06-27 11:19:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-20 06:51:13 +08:00
|
|
|
/// FindExpectedDiags - Lex the main source file to find all of the
|
|
|
|
// expected errors and warnings.
|
|
|
|
static void FindExpectedDiags(Preprocessor &PP,
|
2007-06-28 13:17:33 +08:00
|
|
|
DiagList &ExpectedErrors,
|
2008-09-11 10:46:36 +08:00
|
|
|
DiagList &ExpectedWarnings,
|
|
|
|
DiagList &ExpectedNotes) {
|
2008-11-21 09:18:36 +08:00
|
|
|
// Create a raw lexer to pull all the comments out of the main file. We don't
|
|
|
|
// want to look in #include'd headers for expected-error strings.
|
2009-01-17 14:22:33 +08:00
|
|
|
FileID FID = PP.getSourceManager().getMainFileID();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-21 09:18:36 +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 = PP.getSourceManager().getBuffer(FID);
|
|
|
|
Lexer RawLex(FID, FromFile, PP.getSourceManager(), PP.getLangOptions());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-06-28 12:54:17 +08:00
|
|
|
// Return comments as tokens, this is how we find expected diagnostics.
|
2008-11-21 09:18:36 +08:00
|
|
|
RawLex.SetCommentRetentionState(true);
|
2007-06-27 11:19:45 +08:00
|
|
|
|
2007-07-21 00:59:19 +08:00
|
|
|
Token Tok;
|
2008-11-24 07:38:26 +08:00
|
|
|
Tok.setKind(tok::comment);
|
|
|
|
while (Tok.isNot(tok::eof)) {
|
2008-11-21 09:18:36 +08:00
|
|
|
RawLex.Lex(Tok);
|
2008-11-24 07:38:26 +08:00
|
|
|
if (!Tok.is(tok::comment)) continue;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-24 07:38:26 +08:00
|
|
|
std::string Comment = PP.getSpelling(Tok);
|
2008-11-24 09:28:17 +08:00
|
|
|
if (Comment.empty()) continue;
|
2008-11-24 07:38:26 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-24 07:38:26 +08:00
|
|
|
// Find all expected errors.
|
2008-11-24 09:28:17 +08:00
|
|
|
FindDiagnostics(&Comment[0], Comment.size(), ExpectedErrors, PP,
|
2008-11-24 07:38:26 +08:00
|
|
|
Tok.getLocation(), "expected-error");
|
|
|
|
|
|
|
|
// Find all expected warnings.
|
2008-11-24 09:28:17 +08:00
|
|
|
FindDiagnostics(&Comment[0], Comment.size(), ExpectedWarnings, PP,
|
2008-11-24 07:38:26 +08:00
|
|
|
Tok.getLocation(), "expected-warning");
|
|
|
|
|
|
|
|
// Find all expected notes.
|
2008-11-24 09:28:17 +08:00
|
|
|
FindDiagnostics(&Comment[0], Comment.size(), ExpectedNotes, PP,
|
2008-11-24 07:38:26 +08:00
|
|
|
Tok.getLocation(), "expected-note");
|
|
|
|
};
|
2007-06-27 11:19:45 +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".
|
2009-09-09 23:08:12 +08:00
|
|
|
///
|
2007-06-27 11:19:45 +08:00
|
|
|
static bool PrintProblem(SourceManager &SourceMgr,
|
|
|
|
const_diag_iterator diag_begin,
|
|
|
|
const_diag_iterator diag_end,
|
|
|
|
const char *Msg) {
|
|
|
|
if (diag_begin == diag_end) return false;
|
|
|
|
|
2009-12-03 17:14:02 +08:00
|
|
|
llvm::errs() << Msg << "\n";
|
2007-06-27 12:06:59 +08:00
|
|
|
for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I)
|
2009-12-03 17:14:02 +08:00
|
|
|
llvm::errs() << " Line " << SourceMgr.getInstantiationLineNumber(I->first)
|
|
|
|
<< " " << I->second << "\n";
|
2007-06-27 11:19:45 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-02-04 10:15:34 +08:00
|
|
|
/// CompareDiagLists - Compare two diagnostic lists and return the difference
|
2007-06-27 11:19:45 +08:00
|
|
|
/// between them.
|
2009-09-09 23:08:12 +08:00
|
|
|
///
|
2007-06-27 11:19:45 +08:00
|
|
|
static bool CompareDiagLists(SourceManager &SourceMgr,
|
|
|
|
const_diag_iterator d1_begin,
|
|
|
|
const_diag_iterator d1_end,
|
|
|
|
const_diag_iterator d2_begin,
|
|
|
|
const_diag_iterator d2_end,
|
2009-02-08 03:52:04 +08:00
|
|
|
const char *MsgLeftOnly,
|
|
|
|
const char *MsgRightOnly) {
|
|
|
|
DiagList LeftOnly;
|
|
|
|
DiagList Left(d1_begin, d1_end);
|
|
|
|
DiagList Right(d2_begin, d2_end);
|
2007-06-27 11:19:45 +08:00
|
|
|
|
2009-02-08 03:52:04 +08:00
|
|
|
for (const_diag_iterator I = Left.begin(), E = Left.end(); I != E; ++I) {
|
2009-01-16 15:36:28 +08:00
|
|
|
unsigned LineNo1 = SourceMgr.getInstantiationLineNumber(I->first);
|
2007-06-27 12:06:59 +08:00
|
|
|
const std::string &Diag1 = I->second;
|
2007-06-27 11:19:45 +08:00
|
|
|
|
2009-02-08 03:52:04 +08:00
|
|
|
DiagList::iterator II, IE;
|
|
|
|
for (II = Right.begin(), IE = Right.end(); II != IE; ++II) {
|
2009-01-16 15:36:28 +08:00
|
|
|
unsigned LineNo2 = SourceMgr.getInstantiationLineNumber(II->first);
|
2007-06-27 15:43:27 +08:00
|
|
|
if (LineNo1 != LineNo2) continue;
|
2007-06-27 12:06:59 +08:00
|
|
|
|
2007-06-27 15:43:27 +08:00
|
|
|
const std::string &Diag2 = II->second;
|
2007-06-27 12:06:59 +08:00
|
|
|
if (Diag2.find(Diag1) != std::string::npos ||
|
|
|
|
Diag1.find(Diag2) != std::string::npos) {
|
2007-06-27 11:19:45 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-02-08 03:52:04 +08:00
|
|
|
if (II == IE) {
|
|
|
|
// Not found.
|
|
|
|
LeftOnly.push_back(*I);
|
|
|
|
} else {
|
|
|
|
// Found. The same cannot be found twice.
|
|
|
|
Right.erase(II);
|
|
|
|
}
|
2007-06-27 11:19:45 +08:00
|
|
|
}
|
2009-02-08 03:52:04 +08:00
|
|
|
// Now all that's left in Right are those that were not matched.
|
2007-06-27 11:19:45 +08:00
|
|
|
|
2009-02-08 03:52:04 +08:00
|
|
|
return PrintProblem(SourceMgr, LeftOnly.begin(), LeftOnly.end(), MsgLeftOnly)
|
|
|
|
| PrintProblem(SourceMgr, Right.begin(), Right.end(), MsgRightOnly);
|
2007-06-27 11:19:45 +08:00
|
|
|
}
|
|
|
|
|
2007-06-28 13:17:33 +08:00
|
|
|
/// CheckResults - This compares the expected results to those that
|
2007-06-27 11:19:45 +08:00
|
|
|
/// were actually reported. It emits any discrepencies. Return "true" if there
|
|
|
|
/// were problems. Return "false" otherwise.
|
2009-09-09 23:08:12 +08:00
|
|
|
///
|
2007-06-28 13:17:33 +08:00
|
|
|
static bool CheckResults(Preprocessor &PP,
|
|
|
|
const DiagList &ExpectedErrors,
|
2008-09-11 10:46:36 +08:00
|
|
|
const DiagList &ExpectedWarnings,
|
|
|
|
const DiagList &ExpectedNotes) {
|
2008-08-11 03:59:06 +08:00
|
|
|
const DiagnosticClient *DiagClient = PP.getDiagnostics().getClient();
|
|
|
|
assert(DiagClient != 0 &&
|
|
|
|
"DiagChecker requires a valid TextDiagnosticBuffer");
|
2007-06-28 13:17:33 +08:00
|
|
|
const TextDiagnosticBuffer &Diags =
|
2008-08-11 03:59:06 +08:00
|
|
|
static_cast<const TextDiagnosticBuffer&>(*DiagClient);
|
2007-06-28 13:17:33 +08:00
|
|
|
SourceManager &SourceMgr = PP.getSourceManager();
|
|
|
|
|
2007-06-27 11:19:45 +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
|
|
|
|
bool HadProblem = false;
|
|
|
|
|
2009-02-08 03:52:04 +08:00
|
|
|
// See if there are error mismatches.
|
2007-06-27 11:19:45 +08:00
|
|
|
HadProblem |= CompareDiagLists(SourceMgr,
|
|
|
|
ExpectedErrors.begin(), ExpectedErrors.end(),
|
2007-06-28 13:17:33 +08:00
|
|
|
Diags.err_begin(), Diags.err_end(),
|
2009-02-08 03:52:04 +08:00
|
|
|
"Errors expected but not seen:",
|
2007-06-27 11:19:45 +08:00
|
|
|
"Errors seen but not expected:");
|
|
|
|
|
2009-02-08 03:52:04 +08:00
|
|
|
// See if there are warning mismatches.
|
2007-06-27 11:19:45 +08:00
|
|
|
HadProblem |= CompareDiagLists(SourceMgr,
|
|
|
|
ExpectedWarnings.begin(),
|
|
|
|
ExpectedWarnings.end(),
|
2007-06-28 13:17:33 +08:00
|
|
|
Diags.warn_begin(), Diags.warn_end(),
|
2009-02-08 03:52:04 +08:00
|
|
|
"Warnings expected but not seen:",
|
2007-06-27 11:19:45 +08:00
|
|
|
"Warnings seen but not expected:");
|
|
|
|
|
2009-02-08 03:52:04 +08:00
|
|
|
// See if there are note mismatches.
|
2008-09-11 10:46:36 +08:00
|
|
|
HadProblem |= CompareDiagLists(SourceMgr,
|
|
|
|
ExpectedNotes.begin(),
|
|
|
|
ExpectedNotes.end(),
|
|
|
|
Diags.note_begin(), Diags.note_end(),
|
2009-02-08 03:52:04 +08:00
|
|
|
"Notes expected but not seen:",
|
2008-09-11 10:46:36 +08:00
|
|
|
"Notes seen but not expected:");
|
|
|
|
|
2007-06-27 11:19:45 +08:00
|
|
|
return HadProblem;
|
|
|
|
}
|
2007-06-28 12:54:17 +08:00
|
|
|
|
2007-08-11 02:27:41 +08:00
|
|
|
|
2008-06-13 20:15:34 +08:00
|
|
|
/// CheckDiagnostics - Gather the expected diagnostics and check them.
|
|
|
|
bool clang::CheckDiagnostics(Preprocessor &PP) {
|
2007-06-28 12:54:17 +08:00
|
|
|
// Gather the set of expected diagnostics.
|
2008-09-11 10:46:36 +08:00
|
|
|
DiagList ExpectedErrors, ExpectedWarnings, ExpectedNotes;
|
|
|
|
FindExpectedDiags(PP, ExpectedErrors, ExpectedWarnings, ExpectedNotes);
|
2007-09-26 02:37:20 +08:00
|
|
|
|
2007-06-28 13:17:33 +08:00
|
|
|
// Check that the expected diagnostics occurred.
|
2008-09-11 10:46:36 +08:00
|
|
|
return CheckResults(PP, ExpectedErrors, ExpectedWarnings, ExpectedNotes);
|
2007-06-28 12:54:17 +08:00
|
|
|
}
|