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
|
|
|
|
//
|
|
|
|
// This file was developed by Bill Wendling and is distributed under the
|
|
|
|
// University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Process the input files and check that the diagnostic messages are expected.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-06-28 12:54:17 +08:00
|
|
|
#include "clang.h"
|
2007-06-27 11:19:45 +08:00
|
|
|
#include "ASTStreamers.h"
|
2007-06-28 12:54:17 +08:00
|
|
|
#include "TextDiagnosticBuffer.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
|
|
|
|
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.
|
|
|
|
|
|
|
|
static const char * const ExpectedErrStr = "expected-error";
|
|
|
|
static const char * const ExpectedWarnStr = "expected-warning";
|
|
|
|
|
|
|
|
/// FindDiagnostics - Go through the comment and see if it indicates expected
|
|
|
|
/// diagnostics. If so, then put them in a diagnostic list.
|
|
|
|
///
|
|
|
|
static void FindDiagnostics(const std::string &Comment,
|
|
|
|
DiagList &ExpectedDiags,
|
|
|
|
SourceManager &SourceMgr,
|
|
|
|
SourceLocation Pos,
|
|
|
|
const char * const ExpectedStr) {
|
|
|
|
// Find all expected diagnostics
|
|
|
|
typedef std::string::size_type size_type;
|
|
|
|
size_type ColNo = std::string::npos;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
ColNo = Comment.find(ExpectedStr, ColNo);
|
|
|
|
if (ColNo == std::string::npos) break;
|
|
|
|
|
|
|
|
size_type OpenDiag = Comment.find_first_of("{{", ColNo);
|
|
|
|
|
|
|
|
if (OpenDiag == std::string::npos) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"oops:%d: Cannot find beginning of expected error string\n",
|
2007-07-21 00:37:10 +08:00
|
|
|
SourceMgr.getLogicalLineNumber(Pos));
|
2007-06-27 11:19:45 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
OpenDiag += 2;
|
|
|
|
size_type CloseDiag = Comment.find_first_of("}}", OpenDiag);
|
|
|
|
|
|
|
|
if (CloseDiag == std::string::npos) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"oops:%d: Cannot find end of expected error string\n",
|
2007-07-21 00:37:10 +08:00
|
|
|
SourceMgr.getLogicalLineNumber(Pos));
|
2007-06-27 11:19:45 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Msg(Comment.substr(OpenDiag, CloseDiag - OpenDiag));
|
|
|
|
ExpectedDiags.push_back(std::make_pair(Pos, Msg));
|
|
|
|
ColNo = CloseDiag + 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-28 13:17:33 +08:00
|
|
|
/// FindExpectedDiags - Lex the file to finds all of the expected errors and
|
|
|
|
/// warnings.
|
|
|
|
static void FindExpectedDiags(Preprocessor &PP, unsigned MainFileID,
|
|
|
|
DiagList &ExpectedErrors,
|
|
|
|
DiagList &ExpectedWarnings) {
|
2007-06-28 12:54:17 +08:00
|
|
|
// Return comments as tokens, this is how we find expected diagnostics.
|
2007-06-27 11:19:45 +08:00
|
|
|
PP.SetCommentRetentionState(true, true);
|
|
|
|
|
|
|
|
// Enter the cave.
|
|
|
|
PP.EnterSourceFile(MainFileID, 0, true);
|
|
|
|
|
2007-06-28 12:54:17 +08:00
|
|
|
LexerToken Tok;
|
2007-06-27 11:19:45 +08:00
|
|
|
do {
|
|
|
|
PP.Lex(Tok);
|
|
|
|
|
|
|
|
if (Tok.getKind() == tok::comment) {
|
|
|
|
std::string Comment = PP.getSpelling(Tok);
|
|
|
|
|
|
|
|
// Find all expected errors
|
2007-06-28 13:10:07 +08:00
|
|
|
FindDiagnostics(Comment, ExpectedErrors,PP.getSourceManager(),
|
2007-06-27 11:19:45 +08:00
|
|
|
Tok.getLocation(), ExpectedErrStr);
|
|
|
|
|
|
|
|
// Find all expected warnings
|
2007-06-28 13:10:07 +08:00
|
|
|
FindDiagnostics(Comment, ExpectedWarnings, PP.getSourceManager(),
|
|
|
|
Tok.getLocation(), ExpectedWarnStr);
|
2007-06-27 11:19:45 +08:00
|
|
|
}
|
|
|
|
} while (Tok.getKind() != tok::eof);
|
|
|
|
|
|
|
|
PP.SetCommentRetentionState(false, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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,
|
|
|
|
const DiagList &ExpectedWarnings) {
|
|
|
|
const TextDiagnosticBuffer &Diags =
|
|
|
|
static_cast<const TextDiagnosticBuffer&>(PP.getDiagnostics().getClient());
|
|
|
|
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:");
|
|
|
|
|
|
|
|
return HadProblem;
|
|
|
|
}
|
2007-06-28 12:54:17 +08:00
|
|
|
|
|
|
|
/// CheckDiagnostics - Implement the -parse-ast-check diagnostic verifier.
|
|
|
|
bool clang::CheckDiagnostics(Preprocessor &PP, unsigned MainFileID) {
|
|
|
|
// Gather the set of expected diagnostics.
|
|
|
|
DiagList ExpectedErrors, ExpectedWarnings;
|
2007-06-28 13:17:33 +08:00
|
|
|
FindExpectedDiags(PP, MainFileID, ExpectedErrors, ExpectedWarnings);
|
2007-06-28 12:54:17 +08:00
|
|
|
|
2007-06-28 13:17:33 +08:00
|
|
|
// Parse the specified input file.
|
|
|
|
BuildASTs(PP, MainFileID, false);
|
|
|
|
|
|
|
|
// Check that the expected diagnostics occurred.
|
|
|
|
return CheckResults(PP, ExpectedErrors, ExpectedWarnings);
|
2007-06-28 12:54:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|