Skip to content

Commit 72390c5

Browse files
authored
[clang-tidy][misleading-indentation]ignore false-positives for line started with empty macro (#75061)
Fixes: #71767
1 parent 4972a19 commit 72390c5

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

clang-tools-extra/clang-tidy/readability/MisleadingIndentationCheck.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "MisleadingIndentationCheck.h"
10+
#include "../utils/LexerUtils.h"
1011
#include "clang/AST/ASTContext.h"
1112
#include "clang/ASTMatchers/ASTMatchFinder.h"
1213

@@ -51,8 +52,20 @@ void MisleadingIndentationCheck::danglingElseCheck(const SourceManager &SM,
5152
diag(ElseLoc, "different indentation for 'if' and corresponding 'else'");
5253
}
5354

54-
void MisleadingIndentationCheck::missingBracesCheck(const SourceManager &SM,
55-
const CompoundStmt *CStmt) {
55+
static bool isAtStartOfLineIncludingEmptyMacro(SourceLocation NextLoc,
56+
const SourceManager &SM,
57+
const LangOptions &LangOpts) {
58+
const SourceLocation BeforeLoc =
59+
utils::lexer::getPreviousTokenAndStart(NextLoc, SM, LangOpts).second;
60+
if (BeforeLoc.isInvalid())
61+
return false;
62+
return SM.getExpansionLineNumber(BeforeLoc) !=
63+
SM.getExpansionLineNumber(NextLoc);
64+
}
65+
66+
void MisleadingIndentationCheck::missingBracesCheck(
67+
const SourceManager &SM, const CompoundStmt *CStmt,
68+
const LangOptions &LangOpts) {
5669
const static StringRef StmtNames[] = {"if", "for", "while"};
5770
for (unsigned int I = 0; I < CStmt->size() - 1; I++) {
5871
const Stmt *CurrentStmt = CStmt->body_begin()[I];
@@ -92,6 +105,8 @@ void MisleadingIndentationCheck::missingBracesCheck(const SourceManager &SM,
92105

93106
if (NextLoc.isInvalid() || NextLoc.isMacroID())
94107
continue;
108+
if (!isAtStartOfLineIncludingEmptyMacro(NextLoc, SM, LangOpts))
109+
continue;
95110

96111
if (SM.getExpansionColumnNumber(InnerLoc) ==
97112
SM.getExpansionColumnNumber(NextLoc)) {
@@ -117,7 +132,8 @@ void MisleadingIndentationCheck::check(const MatchFinder::MatchResult &Result) {
117132
danglingElseCheck(*Result.SourceManager, Result.Context, If);
118133

119134
if (const auto *CStmt = Result.Nodes.getNodeAs<CompoundStmt>("compound"))
120-
missingBracesCheck(*Result.SourceManager, CStmt);
135+
missingBracesCheck(*Result.SourceManager, CStmt,
136+
Result.Context->getLangOpts());
121137
}
122138

123139
} // namespace clang::tidy::readability

clang-tools-extra/clang-tidy/readability/MisleadingIndentationCheck.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ class MisleadingIndentationCheck : public ClangTidyCheck {
3232
private:
3333
void danglingElseCheck(const SourceManager &SM, ASTContext *Context,
3434
const IfStmt *If);
35-
void missingBracesCheck(const SourceManager &SM, const CompoundStmt *CStmt);
35+
void missingBracesCheck(const SourceManager &SM, const CompoundStmt *CStmt,
36+
const LangOptions &LangOpts);
3637
};
3738

3839
} // namespace clang::tidy::readability

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,10 @@ Changes in existing checks
465465
`AllowPointerConditions` options. It also now provides more consistent
466466
suggestions when parentheses are added to the return value.
467467

468+
- Improved :doc:`readability-misleading-indentation
469+
<clang-tidy/checks/readability/misleading-indentation>` check to ignore
470+
false-positives for line started with empty macro.
471+
468472
- Improved :doc:`readability-non-const-parameter
469473
<clang-tidy/checks/readability/non-const-parameter>` check to ignore
470474
false-positives in initializer list of record.

clang-tools-extra/test/clang-tidy/checkers/readability/misleading-indentation.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ void foo1();
44
void foo2();
55
void foo3();
66

7+
#define E
8+
79
#define BLOCK \
810
if (cond1) \
911
foo1(); \
@@ -109,6 +111,13 @@ void f()
109111
}
110112

111113
BLOCK
114+
115+
if (cond1)
116+
foo1();
117+
else
118+
foo2();
119+
E foo3();
120+
// CHECK-MESSAGES-NOT: :[[@LINE-1]]readability-misleading-indentation
112121
}
113122

114123
void g(bool x) {

0 commit comments

Comments
 (0)