Skip to content

Commit 576e5b3

Browse files
salman-javed-nztstellar
authored andcommitted
[clang-tidy] Fix #55134 (regression introduced by 5da7c04)
5da7c04 introduced a regression in the NOLINT macro checking loop, replacing the call to `getImmediateExpansionRange().getBegin()` with `getImmediateMacroCallerLoc()`, which has similar but subtly different behaviour. The consequence is that NOLINTs cannot suppress diagnostics when they are attached to a token that came from a macro **argument**, rather than elsewhere in the macro expansion. Revert to pre-patch behaviour and add test cases to cover this issue. Differential Revision: https://reviews.llvm.org/D126138 (cherry picked from commit 9ff4f2d)
1 parent 99b5eb2 commit 576e5b3

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

clang-tools-extra/clang-tidy/NoLintDirectiveHandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ bool NoLintDirectiveHandler::Impl::diagHasNoLintInMacro(
266266
return true;
267267
if (!DiagLoc.isMacroID())
268268
return false;
269-
DiagLoc = SrcMgr.getImmediateMacroCallerLoc(DiagLoc);
269+
DiagLoc = SrcMgr.getImmediateExpansionRange(DiagLoc).getBegin();
270270
}
271271
return false;
272272
}

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ Improvements to clang-tidy
210210

211211
- Added support for external plugin checks with `-load`.
212212

213+
- Fixed a regression introduced in clang-tidy 14.0.0, which prevented NOLINTs
214+
from suppressing diagnostics associated with macro arguments. This fixes
215+
`Issue 55134 <https://github.com/llvm/llvm-project/issues/55134>`_.
216+
213217
New checks
214218
^^^^^^^^^^
215219

clang-tools-extra/test/clang-tidy/infrastructure/nolint.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// REQUIRES: static-analyzer
2-
// RUN: %check_clang_tidy %s google-explicit-constructor,clang-diagnostic-unused-variable,clang-analyzer-core.UndefinedBinaryOperatorResult,modernize-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays %t -- -extra-arg=-Wunused-variable -- -I%S/Inputs/nolint
2+
// RUN: %check_clang_tidy %s google-explicit-constructor,clang-diagnostic-unused-variable,clang-analyzer-core.UndefinedBinaryOperatorResult,modernize-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays,cppcoreguidelines-pro-type-member-init %t -- -extra-arg=-Wunused-variable -- -I%S/Inputs/nolint
33

44
#include "trigger_warning.h"
55
void I(int& Out) {
@@ -96,6 +96,23 @@ MACRO_NOARG // NOLINT
9696
#define MACRO_NOLINT class G { G(int i); }; // NOLINT
9797
MACRO_NOLINT
9898

99+
// Check that we can suppress diagnostics about macro arguments (as opposed to
100+
// diagnostics about the macro contents itself).
101+
#define MACRO_SUPPRESS_DIAG_FOR_ARG_1(X) \
102+
class X { \
103+
X(int i); /* NOLINT(google-explicit-constructor) */ \
104+
};
105+
106+
MACRO_SUPPRESS_DIAG_FOR_ARG_1(G1)
107+
108+
#define MACRO_SUPPRESS_DIAG_FOR_ARG_2(X) \
109+
struct X { /* NOLINT(cppcoreguidelines-pro-type-member-init) */ \
110+
int a = 0; \
111+
int b; \
112+
};
113+
114+
MACRO_SUPPRESS_DIAG_FOR_ARG_2(G2)
115+
99116
#define DOUBLE_MACRO MACRO(H) // NOLINT
100117
DOUBLE_MACRO
101118

@@ -116,4 +133,4 @@ int array2[10]; // NOLINT(cppcoreguidelines-avoid-c-arrays)
116133
int array3[10]; // NOLINT(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays)
117134
int array4[10]; // NOLINT(*-avoid-c-arrays)
118135

119-
// CHECK-MESSAGES: Suppressed 34 warnings (34 NOLINT)
136+
// CHECK-MESSAGES: Suppressed 36 warnings (36 NOLINT)

0 commit comments

Comments
 (0)