class SDiagsWriter : public DiagnosticConsumer {
public:
- SDiagsWriter(DiagnosticsEngine &diags, llvm::raw_ostream *os)
- : LangOpts(0), Stream(Buffer), OS(os), Diags(diags),
- inNonNoteDiagnostic(false)
+ explicit SDiagsWriter(llvm::raw_ostream *os)
+ : LangOpts(0), Stream(Buffer), OS(os), inNonNoteDiagnostic(false)
{
EmitPreamble();
}
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
const Diagnostic &Info);
- void EndSourceFile();
-
void BeginSourceFile(const LangOptions &LO,
const Preprocessor *PP) {
LangOpts = &LO;
}
-
+
+ virtual void finish();
+
DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
// It makes no sense to clone this.
return 0;
void EmitMetaBlock();
/// \brief Emit a record for a CharSourceRange.
- void EmitCharSourceRange(CharSourceRange R);
+ void EmitCharSourceRange(CharSourceRange R, SourceManager &SM);
/// \brief Emit the string information for the category for a diagnostic.
unsigned getEmitCategory(unsigned DiagID);
const Diagnostic &Info);
/// \brief Emit (lazily) the file string and retrieved the file identifier.
- unsigned getEmitFile(SourceLocation Loc);
+ unsigned getEmitFile(SourceLocation Loc, SourceManager &SM);
/// \brief Add SourceLocation information the specified record.
void AddLocToRecord(SourceLocation Loc, RecordDataImpl &Record,
+ SourceManager &SM,
unsigned TokSize = 0);
/// \brief Add CharSourceRange information the specified record.
- void AddCharSourceRangeToRecord(CharSourceRange R, RecordDataImpl &Record);
+ void AddCharSourceRangeToRecord(CharSourceRange R, RecordDataImpl &Record,
+ SourceManager &SM);
/// \brief The version of the diagnostics file.
enum { Version = 1 };
/// \brief The name of the diagnostics file.
llvm::OwningPtr<llvm::raw_ostream> OS;
- /// \brief The DiagnosticsEngine tied to all diagnostic locations.
- DiagnosticsEngine &Diags;
-
/// \brief The set of constructed record abbreviations.
AbbreviationMap Abbrevs;
namespace clang {
namespace serialized_diags {
-DiagnosticConsumer *create(llvm::raw_ostream *OS, DiagnosticsEngine &Diags) {
- return new SDiagsWriter(Diags, OS);
+DiagnosticConsumer *create(llvm::raw_ostream *OS) {
+ return new SDiagsWriter(OS);
}
} // end namespace serialized_diags
} // end namespace clang
void SDiagsWriter::AddLocToRecord(SourceLocation Loc,
RecordDataImpl &Record,
+ SourceManager &SM,
unsigned TokSize) {
if (Loc.isInvalid()) {
// Emit a "sentinel" location.
return;
}
- SourceManager &SM = Diags.getSourceManager();
Loc = SM.getSpellingLoc(Loc);
- Record.push_back(getEmitFile(Loc));
+ Record.push_back(getEmitFile(Loc, SM));
Record.push_back(SM.getSpellingLineNumber(Loc));
Record.push_back(SM.getSpellingColumnNumber(Loc)+TokSize);
Record.push_back(SM.getFileOffset(Loc));
}
void SDiagsWriter::AddCharSourceRangeToRecord(CharSourceRange Range,
- RecordDataImpl &Record) {
- AddLocToRecord(Range.getBegin(), Record);
+ RecordDataImpl &Record,
+ SourceManager &SM) {
+ AddLocToRecord(Range.getBegin(), Record, SM);
unsigned TokSize = 0;
if (Range.isTokenRange())
TokSize = Lexer::MeasureTokenLength(Range.getEnd(),
- Diags.getSourceManager(),
- *LangOpts);
+ SM, *LangOpts);
- AddLocToRecord(Range.getEnd(), Record, TokSize);
+ AddLocToRecord(Range.getEnd(), Record, SM, TokSize);
}
-unsigned SDiagsWriter::getEmitFile(SourceLocation Loc) {
- SourceManager &SM = Diags.getSourceManager();
+unsigned SDiagsWriter::getEmitFile(SourceLocation Loc, SourceManager &SM) {
assert(Loc.isValid());
const std::pair<FileID, unsigned> &LocInfo = SM.getDecomposedLoc(Loc);
const FileEntry *FE = SM.getFileEntryForID(LocInfo.first);
return entry;
}
-void SDiagsWriter::EmitCharSourceRange(CharSourceRange R) {
+void SDiagsWriter::EmitCharSourceRange(CharSourceRange R,
+ SourceManager &SM) {
Record.clear();
Record.push_back(RECORD_SOURCE_RANGE);
- AddCharSourceRangeToRecord(R, Record);
+ AddCharSourceRangeToRecord(R, Record, SM);
Stream.EmitRecordWithAbbrev(Abbrevs.get(RECORD_SOURCE_RANGE), Record);
}
void SDiagsWriter::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
const Diagnostic &Info) {
+ SourceManager &SM = Info.getSourceManager();
if (DiagLevel != DiagnosticsEngine::Note) {
if (inNonNoteDiagnostic) {
Record.clear();
Record.push_back(RECORD_DIAG);
Record.push_back(DiagLevel);
- AddLocToRecord(Info.getLocation(), Record);
+ AddLocToRecord(Info.getLocation(), Record, SM);
// Emit the category string lazily and get the category ID.
Record.push_back(getEmitCategory(Info.getID()));
// Emit the diagnostic flag string lazily and get the mapped ID.
ArrayRef<CharSourceRange> Ranges = Info.getRanges();
for (ArrayRef<CharSourceRange>::iterator it=Ranges.begin(), ei=Ranges.end();
it != ei; ++it) {
- EmitCharSourceRange(*it);
+ if (it->isValid())
+ EmitCharSourceRange(*it, SM);
}
// Emit FixIts.
continue;
Record.clear();
Record.push_back(RECORD_FIXIT);
- AddCharSourceRangeToRecord(fix.RemoveRange, Record);
+ AddCharSourceRangeToRecord(fix.RemoveRange, Record, SM);
Record.push_back(fix.CodeToInsert.size());
Stream.EmitRecordWithBlob(Abbrevs.get(RECORD_FIXIT), Record,
fix.CodeToInsert);
}
}
-void SDiagsWriter::EndSourceFile() {
+void SDiagsWriter::finish() {
if (inNonNoteDiagnostic) {
// Finish off any diagnostics we were in the process of emitting.
Stream.ExitBlock();
// CHECK: {{.*}}serialized-diags-single-issue.c:3:12: warning: variable 'voodoo' is uninitialized when used here [-Wuninitialized]
// CHECK: Range: {{.*}}serialized-diags-single-issue.c:3:12 {{.*}}serialized-diags-single-issue.c:3:18
// CHECK: +-{{.*}}serialized-diags-single-issue.c:2:13: note: initialize the variable 'voodoo' to silence this warning []
-// CHECK: +-FIXIT: {{.*}}serialized-diags-single-issue.c:2:13 - {{.*}}serialized-diags-single-issue.c:2:13): " = 0"
\ No newline at end of file
+// CHECK: +-FIXIT: {{.*}}serialized-diags-single-issue.c:2:13 - {{.*}}serialized-diags-single-issue.c:2:13): " = 0"
+
+// Test that we handle serializing diagnostics for multiple source files
+// RUN: %clang_cc1 -Wall -fsyntax-only %s %s -serialize-diagnostic-file %t
+// RUN: c-index-test -read-diagnostics %t 2>&1 | FileCheck -check-prefix=CHECK-MULT %s
+// RUN: rm -f %t
+
+// CHECK-MULT: {{.*}}serialized-diags-single-issue.c:3:12: warning: variable 'voodoo' is uninitialized when used here [-Wuninitialized]
+// CHECK-MULT: Range: {{.*}}serialized-diags-single-issue.c:3:12 {{.*}}serialized-diags-single-issue.c:3:18
+// CHECK-MULT: +-{{.*}}serialized-diags-single-issue.c:2:13: note: initialize the variable 'voodoo' to silence this warning []
+// CHECK-MULT: +-FIXIT: {{.*}}serialized-diags-single-issue.c:2:13 - {{.*}}serialized-diags-single-issue.c:2:13): " = 0"
+
+// CHECK-MULT: {{.*}}serialized-diags-single-issue.c:3:12: warning: variable 'voodoo' is uninitialized when used here [-Wuninitialized]
+// CHECK-MULT: Range: {{.*}}serialized-diags-single-issue.c:3:12 {{.*}}serialized-diags-single-issue.c:3:18
+// CHECK-MULT: +-{{.*}}serialized-diags-single-issue.c:2:13: note: initialize the variable 'voodoo' to silence this warning []
+// CHECK-MULT: +-FIXIT: {{.*}}serialized-diags-single-issue.c:2:13 - {{.*}}serialized-diags-single-issue.c:2:13): " = 0"
+
+// CHECK-MULT: Number of diagnostics: 2