Skip to content

Commit d78d1dc

Browse files
committed
[ClangImporter] Only suppress specific diags in synthetic buffers
We don't want to show "included from <bridging-header>" to the user-- that's just not helpful--but we /do/ want to see any unexpected diagnostics that happen to be reported in the importer's synthetic buffers. This would have helped us track down rdar://problem/34664596 much sooner.
1 parent f331060 commit d78d1dc

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

lib/ClangImporter/ClangDiagnosticConsumer.cpp

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "swift/AST/DiagnosticsClangImporter.h"
1818
#include "clang/AST/ASTContext.h"
1919
#include "clang/Frontend/DiagnosticRenderer.h"
20+
#include "clang/Frontend/FrontendDiagnostic.h"
2021
#include "clang/Lex/LexDiagnostic.h"
2122
#include "llvm/ADT/STLExtras.h"
2223

@@ -36,17 +37,37 @@ namespace {
3637
callback(fn) {}
3738

3839
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+
3963
void emitDiagnosticMessage(clang::FullSourceLoc Loc,
4064
clang::PresumedLoc PLoc,
4165
clang::DiagnosticsEngine::Level Level,
4266
StringRef Message,
4367
ArrayRef<clang::CharSourceRange> Ranges,
4468
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))
4870
return;
49-
}
5071
callback(Loc, Level, Message);
5172
}
5273

@@ -61,8 +82,9 @@ namespace {
6182

6283
void emitNote(clang::FullSourceLoc Loc, StringRef Message) override {
6384
// 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))
6688
return;
6789
emitDiagnosticMessage(Loc, {}, clang::DiagnosticsEngine::Note, Message,
6890
{}, {});
@@ -227,6 +249,7 @@ void ClangDiagnosticConsumer::HandleDiagnostic(
227249
clang::FullSourceLoc clangDiagLoc(clangDiag.getLocation(),
228250
clangDiag.getSourceManager());
229251
renderer.emitDiagnostic(clangDiagLoc, clangDiagLevel, message,
230-
clangDiag.getRanges(), clangDiag.getFixItHints());
252+
clangDiag.getRanges(), clangDiag.getFixItHints(),
253+
&clangDiag);
231254
}
232255
}

0 commit comments

Comments
 (0)