17
17
#include " swift/AST/DiagnosticsClangImporter.h"
18
18
#include " clang/AST/ASTContext.h"
19
19
#include " clang/Frontend/DiagnosticRenderer.h"
20
+ #include " clang/Frontend/FrontendDiagnostic.h"
20
21
#include " clang/Lex/LexDiagnostic.h"
21
22
#include " llvm/ADT/STLExtras.h"
22
23
@@ -36,17 +37,37 @@ namespace {
36
37
callback(fn) {}
37
38
38
39
private:
40
+ // / Is this a diagnostic that doesn't do the user any good to show if it
41
+ // / is located in one of Swift's synthetic buffers? If so, returns true to
42
+ // / suppress it.
43
+ static bool shouldSuppressDiagInSwiftBuffers (clang::DiagOrStoredDiag info) {
44
+ if (info.isNull ())
45
+ return false ;
46
+
47
+ unsigned ID;
48
+ if (auto *activeDiag = info.dyn_cast <const clang::Diagnostic *>())
49
+ ID = activeDiag->getID ();
50
+ else
51
+ ID = info.get <const clang::StoredDiagnostic *>()->getID ();
52
+ return ID == clang::diag::note_module_import_here ||
53
+ ID == clang::diag::err_module_not_built;
54
+ }
55
+
56
+ // / Returns true if \p loc is inside one of Swift's synthetic buffers.
57
+ static bool isInSwiftBuffers (clang::FullSourceLoc loc) {
58
+ StringRef bufName = StringRef (loc.getManager ().getBufferName (loc));
59
+ return bufName == ClangImporter::Implementation::moduleImportBufferName ||
60
+ bufName == ClangImporter::Implementation::bridgingHeaderBufferName;
61
+ }
62
+
39
63
void emitDiagnosticMessage (clang::FullSourceLoc Loc,
40
64
clang::PresumedLoc PLoc,
41
65
clang::DiagnosticsEngine::Level Level,
42
66
StringRef Message,
43
67
ArrayRef<clang::CharSourceRange> Ranges,
44
68
clang::DiagOrStoredDiag Info) override {
45
- StringRef bufName = StringRef (Loc.getManager ().getBufferName (Loc));
46
- if (bufName == ClangImporter::Implementation::moduleImportBufferName ||
47
- bufName == ClangImporter::Implementation::bridgingHeaderBufferName) {
69
+ if (shouldSuppressDiagInSwiftBuffers (Info) && isInSwiftBuffers (Loc))
48
70
return ;
49
- }
50
71
callback (Loc, Level, Message);
51
72
}
52
73
@@ -61,8 +82,9 @@ namespace {
61
82
62
83
void emitNote (clang::FullSourceLoc Loc, StringRef Message) override {
63
84
// We get invalid note locations when trying to describe where a module
64
- // is imported and the actual location is in Swift.
65
- if (Loc.isInvalid ())
85
+ // is imported and the actual location is in Swift. We also want to ignore
86
+ // things like "in module X imported from <swift-imported-modules>".
87
+ if (Loc.isInvalid () || isInSwiftBuffers (Loc))
66
88
return ;
67
89
emitDiagnosticMessage (Loc, {}, clang::DiagnosticsEngine::Note, Message,
68
90
{}, {});
@@ -227,6 +249,7 @@ void ClangDiagnosticConsumer::HandleDiagnostic(
227
249
clang::FullSourceLoc clangDiagLoc (clangDiag.getLocation (),
228
250
clangDiag.getSourceManager ());
229
251
renderer.emitDiagnostic (clangDiagLoc, clangDiagLevel, message,
230
- clangDiag.getRanges (), clangDiag.getFixItHints ());
252
+ clangDiag.getRanges (), clangDiag.getFixItHints (),
253
+ &clangDiag);
231
254
}
232
255
}
0 commit comments