[clang-tools-extra] 9f24148 - [clangd] Fix crash in bugprone-bad-signal-to-kill-thread clang-tidy check.
Aleksandr Platonov via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 6 11:46:40 PDT 2020
Author: Aleksandr Platonov
Date: 2020-08-06T21:45:21+03:00
New Revision: 9f24148b212698aca220ac923d215c2073e443ce
URL: https://github.com/llvm/llvm-project/commit/9f24148b212698aca220ac923d215c2073e443ce
DIFF: https://github.com/llvm/llvm-project/commit/9f24148b212698aca220ac923d215c2073e443ce.diff
LOG: [clangd] Fix crash in bugprone-bad-signal-to-kill-thread clang-tidy check.
Inside clangd, clang-tidy checks don't see preprocessor events in the preamble.
This leads to `Token::PtrData == nullptr` for tokens that the macro is defined to.
E.g. `#define SIGTERM 15`:
- Token::Kind == tok::numeric_constant (Token::isLiteral() == true)
- Token::UintData == 2
- Token::PtrData == nullptr
As the result of this, bugprone-bad-signal-to-kill-thread check crashes at null-dereference inside clangd.
Reviewed By: hokein
Differential Revision: https://reviews.llvm.org/D85417
Added:
Modified:
clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
index 0720c7e689a1..3833640b3de3 100644
--- a/clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
@@ -39,7 +39,7 @@ void BadSignalToKillThreadCheck::check(const MatchFinder::MatchResult &Result) {
return llvm::None;
const MacroInfo *MI = PP->getMacroInfo(It->first);
const Token &T = MI->tokens().back();
- if (!T.isLiteral())
+ if (!T.isLiteral() || !T.getLiteralData())
return llvm::None;
StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
diff --git a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
index fcc6d40a988a..e8757079c675 100644
--- a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
+++ b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
@@ -438,6 +438,21 @@ TEST(DiagnosticTest, ClangTidySuppressionCommentTrumpsWarningAsError) {
EXPECT_THAT(TU.build().getDiagnostics(), UnorderedElementsAre());
}
+TEST(DiagnosticTest, ClangTidyNoLiteralDataInMacroToken) {
+ Annotations Main(R"cpp(
+ #define SIGTERM 15
+ using pthread_t = int;
+ int pthread_kill(pthread_t thread, int sig);
+ int func() {
+ pthread_t thread;
+ return pthread_kill(thread, 0);
+ }
+ )cpp");
+ TestTU TU = TestTU::withCode(Main.code());
+ TU.ClangTidyChecks = "-*,bugprone-bad-signal-to-kill-thread";
+ EXPECT_THAT(TU.build().getDiagnostics(), UnorderedElementsAre()); // no-crash
+}
+
TEST(DiagnosticsTest, Preprocessor) {
// This looks like a preamble, but there's an #else in the middle!
// Check that:
More information about the cfe-commits
mailing list