1313
1414using namespace clang ::ast_matchers;
1515
16- namespace clang ::tidy::bugprone {
16+ namespace clang ::tidy::readability {
1717
1818// / Check that at least one branch of the \p If statement is a \c CompoundStmt.
1919static bool shouldHaveBraces (const IfStmt *If) {
@@ -33,11 +33,10 @@ static bool shouldHaveBraces(const IfStmt *If) {
3333
3434void InconsistentIfelseBracesCheck::registerMatchers (MatchFinder *Finder) {
3535 Finder->addMatcher (
36- traverse (TK_IgnoreUnlessSpelledInSource,
37- ifStmt (hasElse (anything ()),
38- unless (isConsteval ()), // 'if consteval' always has braces
39- unless (hasParent (ifStmt ())))
40- .bind (" if_stmt" )),
36+ ifStmt (hasElse (anything ()),
37+ unless (isConsteval ()), // 'if consteval' always has braces
38+ unless (hasParent (ifStmt ())))
39+ .bind (" if_stmt" ),
4140 this );
4241}
4342
@@ -50,40 +49,41 @@ void InconsistentIfelseBracesCheck::check(
5049}
5150
5251void InconsistentIfelseBracesCheck::checkIfStmt (
53- const ast_matchers:: MatchFinder::MatchResult &Result, const IfStmt *If) {
52+ const MatchFinder::MatchResult &Result, const IfStmt *If) {
5453 const Stmt *Then = If->getThen ();
5554 if (const auto *NestedIf = dyn_cast<const IfStmt>(Then)) {
5655 // If the then-branch is a nested IfStmt, first we need to add braces to
5756 // it, then we need to check the inner IfStmt.
58- checkStmt (Result, If->getThen (), If->getRParenLoc (), If->getElseLoc ());
57+ emitDiagnostic (Result, If->getThen (), If->getRParenLoc (), If->getElseLoc ());
5958 if (shouldHaveBraces (NestedIf))
6059 checkIfStmt (Result, NestedIf);
6160 } else if (!isa<CompoundStmt>(Then)) {
62- checkStmt (Result, If->getThen (), If->getRParenLoc (), If->getElseLoc ());
61+ emitDiagnostic (Result, If->getThen (), If->getRParenLoc (), If->getElseLoc ());
6362 }
6463
6564 if (const Stmt *const Else = If->getElse ()) {
6665 if (const auto *NestedIf = dyn_cast<const IfStmt>(Else))
6766 checkIfStmt (Result, NestedIf);
6867 else if (!isa<CompoundStmt>(Else))
69- checkStmt (Result, If->getElse (), If->getElseLoc ());
68+ emitDiagnostic (Result, If->getElse (), If->getElseLoc ());
7069 }
7170}
7271
73- void InconsistentIfelseBracesCheck::checkStmt (
74- const ast_matchers:: MatchFinder::MatchResult &Result, const Stmt *S,
72+ void InconsistentIfelseBracesCheck::emitDiagnostic (
73+ const MatchFinder::MatchResult &Result, const Stmt *S,
7574 SourceLocation StartLoc, SourceLocation EndLocHint) {
7675 const SourceManager &SM = *Result.SourceManager ;
7776 const LangOptions &LangOpts = Result.Context ->getLangOpts ();
7877
79- const utils::BraceInsertionHints Hints =
80- utils::getBraceInsertionsHints (S, LangOpts, SM, StartLoc, EndLocHint);
81- if (Hints) {
82- DiagnosticBuilder Diag = diag (Hints.DiagnosticPos , " <message>" );
83- if (Hints.offersFixIts ()) {
84- Diag << Hints.openingBraceFixIt () << Hints.closingBraceFixIt ();
85- }
78+ if (!StartLoc.isMacroID ()) {
79+ const utils::BraceInsertionHints Hints =
80+ utils::getBraceInsertionsHints (S, LangOpts, SM, StartLoc, EndLocHint);
81+ assert (Hints && Hints.offersFixIts () && " Expected hints or fix-its" );
82+ diag (Hints.DiagnosticPos , " <message>" )
83+ << Hints.openingBraceFixIt () << Hints.closingBraceFixIt ();
84+ } else {
85+ diag (StartLoc, " <message-for-macro-expansions>" ) << StartLoc.isMacroID ();
8686 }
8787}
8888
89- } // namespace clang::tidy::bugprone
89+ } // namespace clang::tidy::readability
0 commit comments