Index: clang-tidy/readability/BracesAroundStatementsCheck.cpp =================================================================== --- clang-tidy/readability/BracesAroundStatementsCheck.cpp +++ clang-tidy/readability/BracesAroundStatementsCheck.cpp @@ -172,7 +172,7 @@ BracesAroundStatementsCheck::findRParenLoc(const IfOrWhileStmt *S, const SourceManager &SM, const ASTContext *Context) { - // Skip macros + // Skip macros. if (S->getLocStart().isMacroID()) return SourceLocation(); @@ -219,13 +219,17 @@ // Already inside braces. return false; } - // Skip macros. - if (S->getLocStart().isMacroID()) - return false; const SourceManager &SM = *Result.SourceManager; const ASTContext *Context = Result.Context; + // Treat macros. + CharSourceRange FileRange = Lexer::makeFileCharRange( + CharSourceRange::getTokenRange(S->getSourceRange()), SM, + Context->getLangOpts()); + if (FileRange.isInvalid()) + return false; + // InitialLoc points at the last token before opening brace to be inserted. assert(InitialLoc.isValid()); SourceLocation StartLoc = @@ -237,7 +241,8 @@ EndLoc = EndLocHint; ClosingInsertion = "} "; } else { - EndLoc = findEndLocation(S->getLocEnd(), SM, Context); + const auto FREnd = FileRange.getEnd().getLocWithOffset(-1); + EndLoc = findEndLocation(FREnd, SM, Context); ClosingInsertion = "\n}"; } Index: test/clang-tidy/readability-braces-around-statements.cpp =================================================================== --- test/clang-tidy/readability-braces-around-statements.cpp +++ test/clang-tidy/readability-braces-around-statements.cpp @@ -171,3 +171,16 @@ // CHECK-FIXES-NEXT: } // CHECK-FIXES-NEXT: } } + +#define M(x) x + +int test_macros(bool b) { + if (b) { + return 1; + } else + M(return 2); + // CHECK-MESSAGES: :[[@LINE-2]]:9: warning: statement should be inside braces + // CHECK-FIXES: } else { + // CHECK-FIXES-NEXT: M(return 2); + // CHECK-FIXES-NEXT: } +} Index: unittests/clang-tidy/ReadabilityModuleTest.cpp =================================================================== --- unittests/clang-tidy/ReadabilityModuleTest.cpp +++ unittests/clang-tidy/ReadabilityModuleTest.cpp @@ -457,6 +457,27 @@ "int main() {\n" " FOR(;;)\n" "}"); + EXPECT_EQ("#define DO_IT ++i\n" + "int i = 0;\n" + "int main() {\n" + " if (false) {\n" + " DO_IT;\n" + " } else if (1 == 2) {\n" + " DO_IT;\n" + " } else {\n" + " DO_IT;\n" + "}\n" + "}", + runCheckOnCode("#define DO_IT ++i\n" + "int i = 0;\n" + "int main() {\n" + " if (false)\n" + " DO_IT;\n" + " else if (1 == 2)\n" + " DO_IT;\n" + " else\n" + " DO_IT;\n" + "}")); } } // namespace test