-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathClangDiagnosticConsumer.h
100 lines (85 loc) · 3.51 KB
/
ClangDiagnosticConsumer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//===--- ClangDiagnosticConsumer.h - Handle Clang diagnostics ---*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_CLANG_DIAGNOSTIC_CONSUMER_H
#define SWIFT_CLANG_DIAGNOSTIC_CONSUMER_H
#include "swift/ClangImporter/ClangImporter.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/IdentifierTable.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "llvm/Support/MemoryBuffer.h"
namespace swift {
class ClangDiagnosticConsumer : public clang::TextDiagnosticPrinter {
struct LoadModuleRAII {
ClangDiagnosticConsumer *Consumer;
clang::DiagnosticsEngine *Engine;
bool DiagEngineClearPriorToLookup;
public:
LoadModuleRAII(ClangDiagnosticConsumer &consumer,
clang::DiagnosticsEngine &engine,
const clang::IdentifierInfo *import)
: Consumer(&consumer), Engine(&engine) {
assert(import);
assert(!Consumer->CurrentImport);
assert(!Consumer->CurrentImportNotFound);
Consumer->CurrentImport = import;
DiagEngineClearPriorToLookup = !engine.hasErrorOccurred();
}
LoadModuleRAII(LoadModuleRAII &) = delete;
LoadModuleRAII &operator=(LoadModuleRAII &) = delete;
LoadModuleRAII(LoadModuleRAII &&other) {
*this = std::move(other);
}
LoadModuleRAII &operator=(LoadModuleRAII &&other) {
Consumer = other.Consumer;
other.Consumer = nullptr;
return *this;
}
~LoadModuleRAII() {
if (Consumer) {
// We must reset Clang's diagnostic engine here since we know that only
// the module lookup errors have been emitted. While the ClangDiagnosticConsumer
// takes care of filtering out the diagnostics from the output and from
// being handled by Swift's DiagnosticEngine, we must ensure to also
// reset Clang's DiagnosticEngine because its state is queried in later
// stages of compilation and errors emitted on "module_not_found" should not
// be counted.
// FIXME: We must instead allow for module loading in Clang to fail without
// needing to emit a diagnostic.
if (Engine && Consumer->CurrentImportNotFound && DiagEngineClearPriorToLookup)
Engine->Reset();
Consumer->CurrentImport = nullptr;
Consumer->CurrentImportNotFound = false;
}
}
};
private:
friend struct LoadModuleRAII;
ClangImporter::Implementation &ImporterImpl;
const clang::IdentifierInfo *CurrentImport = nullptr;
bool CurrentImportNotFound = false;
SourceLoc DiagLoc;
const bool DumpToStderr;
public:
ClangDiagnosticConsumer(ClangImporter::Implementation &impl,
clang::DiagnosticOptions &clangDiagOptions,
bool dumpToStderr);
LoadModuleRAII handleImport(const clang::IdentifierInfo *name,
clang::DiagnosticsEngine &engine,
SourceLoc diagLoc) {
DiagLoc = diagLoc;
return LoadModuleRAII(*this, engine, name);
}
void HandleDiagnostic(clang::DiagnosticsEngine::Level diagLevel,
const clang::Diagnostic &info) override;
};
} // end namespace swift
#endif