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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-06-28 12:54:17 +08:00
|
|
|
#include "clang.h"
|
2007-10-07 14:04:32 +08:00
|
|
|
#include "ASTConsumers.h"
|
2008-08-06 07:33:20 +08:00
|
|
|
#include "clang/Driver/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"
|
|
|
|
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.
|
|
|
|
|
|
|
|
/// FindDiagnostics - Go through the comment and see if it indicates expected
|
|
|
|
/// diagnostics. If so, then put them in a diagnostic list.
|
|
|
|
///
|
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);
|
2008-11-24 07:38:26 +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;
|
|
|
|
|
|
|
|
// If this isn't expected-foo, ignore it.
|
|
|
|
if (memcmp(CommentStart, ExpectedStr, ExpectedStrLen)) {
|
|
|
|
++CommentStart;
|
|
|
|
continue;
|
2007-06-27 11:19:45 +08:00
|
|
|
}
|
2008-11-24 09:28:17 +08:00
|
|
|
|
|
|
|
CommentStart += ExpectedStrLen;
|
|
|
|
|
|
|
|
// Skip whitespace.
|
|
|
|
while (CommentStart != CommentEnd &&
|
|
|
|
isspace(CommentStart[0]))
|
|
|
|
++CommentStart;
|
|
|
|
|
|
|
|
// 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;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
2007-06-27 11:19:45 +08:00
|
|
|
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.
|
|
|
|
unsigned FileID = PP.getSourceManager().getMainFileID();
|
|
|
|
std::pair<const char*,const char*> File =
|
|
|
|
PP.getSourceManager().getBufferData(FileID);
|
|
|
|
|
|
|
|
// Create a lexer to lex all the tokens of the main file in raw mode.
|
|
|
|
Lexer RawLex(SourceLocation::getFileLoc(FileID, 0),
|
|
|
|
PP.getLangOptions(), File.first, File.second);
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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
|
|
|
|
2008-11-24 09:28:17 +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".
|
|
|
|
///
|
|
|
|
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;
|
|
|
|
|
|
|
|
fprintf(stderr, "%s\n", Msg);
|
|
|
|
|
2007-06-27 12:06:59 +08:00
|
|
|
for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I)
|
2007-06-28 01:26:23 +08:00
|
|
|
fprintf(stderr, " Line %d: %s\n",
|
2007-07-21 00:37:10 +08:00
|
|
|
SourceMgr.getLogicalLineNumber(I->first),
|
2007-06-27 12:06:59 +08:00
|
|
|
I->second.c_str());
|
2007-06-27 11:19:45 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CompareDiagLists - Compare two diangnostic lists and return the difference
|
|
|
|
/// between them.
|
|
|
|
///
|
|
|
|
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,
|
|
|
|
const char *Msg) {
|
2007-06-27 15:24:11 +08:00
|
|
|
DiagList DiffList;
|
2007-06-27 11:19:45 +08:00
|
|
|
|
2007-06-27 12:06:59 +08:00
|
|
|
for (const_diag_iterator I = d1_begin, E = d1_end; I != E; ++I) {
|
2007-07-21 00:37:10 +08:00
|
|
|
unsigned LineNo1 = SourceMgr.getLogicalLineNumber(I->first);
|
2007-06-27 12:06:59 +08:00
|
|
|
const std::string &Diag1 = I->second;
|
2007-06-27 11:19:45 +08:00
|
|
|
bool Found = false;
|
|
|
|
|
2007-06-27 12:06:59 +08:00
|
|
|
for (const_diag_iterator II = d2_begin, IE = d2_end; II != IE; ++II) {
|
2007-07-21 00:37:10 +08:00
|
|
|
unsigned LineNo2 = SourceMgr.getLogicalLineNumber(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
|
|
|
Found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Found)
|
2007-06-27 15:43:27 +08:00
|
|
|
DiffList.push_back(std::make_pair(I->first, Diag1));
|
2007-06-27 11:19:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return PrintProblem(SourceMgr, DiffList.begin(), DiffList.end(), Msg);
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
///
|
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;
|
|
|
|
|
|
|
|
// See if there were errors that were expected but not seen.
|
|
|
|
HadProblem |= CompareDiagLists(SourceMgr,
|
|
|
|
ExpectedErrors.begin(), ExpectedErrors.end(),
|
2007-06-28 13:17:33 +08:00
|
|
|
Diags.err_begin(), Diags.err_end(),
|
2007-06-27 11:19:45 +08:00
|
|
|
"Errors expected but not seen:");
|
|
|
|
|
|
|
|
// See if there were errors that were seen but not expected.
|
|
|
|
HadProblem |= CompareDiagLists(SourceMgr,
|
2007-06-28 13:17:33 +08:00
|
|
|
Diags.err_begin(), Diags.err_end(),
|
2007-06-27 11:19:45 +08:00
|
|
|
ExpectedErrors.begin(), ExpectedErrors.end(),
|
|
|
|
"Errors seen but not expected:");
|
|
|
|
|
|
|
|
// See if there were warnings that were expected but not seen.
|
|
|
|
HadProblem |= CompareDiagLists(SourceMgr,
|
|
|
|
ExpectedWarnings.begin(),
|
|
|
|
ExpectedWarnings.end(),
|
2007-06-28 13:17:33 +08:00
|
|
|
Diags.warn_begin(), Diags.warn_end(),
|
2007-06-27 11:19:45 +08:00
|
|
|
"Warnings expected but not seen:");
|
|
|
|
|
|
|
|
// See if there were warnings that were seen but not expected.
|
|
|
|
HadProblem |= CompareDiagLists(SourceMgr,
|
2007-06-28 13:17:33 +08:00
|
|
|
Diags.warn_begin(), Diags.warn_end(),
|
2007-06-27 11:19:45 +08:00
|
|
|
ExpectedWarnings.begin(),
|
|
|
|
ExpectedWarnings.end(),
|
|
|
|
"Warnings seen but not expected:");
|
|
|
|
|
2008-09-11 10:46:36 +08:00
|
|
|
// See if there were notes that were expected but not seen.
|
|
|
|
HadProblem |= CompareDiagLists(SourceMgr,
|
|
|
|
ExpectedNotes.begin(),
|
|
|
|
ExpectedNotes.end(),
|
|
|
|
Diags.note_begin(), Diags.note_end(),
|
|
|
|
"Notes expected but not seen:");
|
|
|
|
|
|
|
|
// See if there were notes that were seen but not expected.
|
|
|
|
HadProblem |= CompareDiagLists(SourceMgr,
|
|
|
|
Diags.note_begin(), Diags.note_end(),
|
|
|
|
ExpectedNotes.begin(),
|
|
|
|
ExpectedNotes.end(),
|
|
|
|
"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
|
|
|
}
|