Skip to content

Commit

Permalink
[clangd] Fix crash in bugprone-bad-signal-to-kill-thread clang-tidy c…
Browse files Browse the repository at this point in the history
…heck.

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
  • Loading branch information
ArcsinX committed Aug 6, 2020
1 parent 4ccc388 commit 9f24148
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
15 changes: 15 additions & 0 deletions clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 9f24148

Please sign in to comment.