[clang-tools-extra] ac31781 - [Sema] Tweak diagnostic logic so suppress-in-header logic works in tools too.
Sam McCall via cfe-commits
cfe-commits at lists.llvm.org
Fri Aug 19 06:19:45 PDT 2022
Author: Sam McCall
Date: 2022-08-19T15:16:10+02:00
New Revision: ac31781759889226711b801c3833fea280fdb7aa
URL: https://github.com/llvm/llvm-project/commit/ac31781759889226711b801c3833fea280fdb7aa
DIFF: https://github.com/llvm/llvm-project/commit/ac31781759889226711b801c3833fea280fdb7aa.diff
LOG: [Sema] Tweak diagnostic logic so suppress-in-header logic works in tools too.
Certain idioms are ignored by -Wunused in header files only.
The current "is a header" check assumes that if headers are the main file, we're
building a PCH or a module or something. However in tools we may be parsing the
header in its own right, but still want to treat it as a header.
Fixes https://github.com/clangd/vscode-clangd/issues/360
Differential Revision: https://reviews.llvm.org/D129642
Added:
Modified:
clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
clang/lib/Sema/SemaDecl.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
index 0a24f3c0c69b..306c8b122109 100644
--- a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
+++ b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
@@ -54,6 +54,9 @@ ::testing::Matcher<const Diag &> withFix(::testing::Matcher<Fix> FixMatcher1,
return Field(&Diag::Fixes, UnorderedElementsAre(FixMatcher1, FixMatcher2));
}
+::testing::Matcher<const Diag &> withID(unsigned ID) {
+ return Field(&Diag::ID, ID);
+}
::testing::Matcher<const Diag &>
withNote(::testing::Matcher<Note> NoteMatcher) {
return Field(&Diag::Notes, ElementsAre(NoteMatcher));
@@ -1869,6 +1872,20 @@ TEST(DiagnosticsTest, FixItFromHeader) {
"'int' to 'int *' for 1st argument; take the address of "
"the argument with &")))));
}
+
+TEST(DiagnosticsTest, UnusedInHeader) {
+ // Clang diagnoses unused static inline functions outside headers.
+ auto TU = TestTU::withCode("static inline void foo(void) {}");
+ TU.ExtraArgs.push_back("-Wunused-function");
+ TU.Filename = "test.c";
+ EXPECT_THAT(*TU.build().getDiagnostics(),
+ ElementsAre(withID(diag::warn_unused_function)));
+ // Sema should recognize a *.h file open in clangd as a header.
+ // https://github.com/clangd/vscode-clangd/issues/360
+ TU.Filename = "test.h";
+ EXPECT_THAT(*TU.build().getDiagnostics(), IsEmpty());
+}
+
} // namespace
} // namespace clangd
} // namespace clang
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index d4c8377bbce7..33b49482c6e5 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -1855,7 +1855,7 @@ bool Sema::mightHaveNonExternalLinkage(const DeclaratorDecl *D) {
// FIXME: This needs to be refactored; some other isInMainFile users want
// these semantics.
static bool isMainFileLoc(const Sema &S, SourceLocation Loc) {
- if (S.TUKind != TU_Complete)
+ if (S.TUKind != TU_Complete || S.getLangOpts().IsHeaderFile)
return false;
return S.SourceMgr.isInMainFile(Loc);
}
More information about the cfe-commits
mailing list