| 1 |
//===--- BracesAroundStatementsCheck.cpp - clang-tidy ---------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include "BracesAroundStatementsCheck.h" |
| 10 |
#include "../utils/LexerUtils.h" |
| 11 |
#include "clang/AST/ASTContext.h" |
| 12 |
#include "clang/ASTMatchers/ASTMatchers.h" |
| 13 |
#include "clang/Lex/Lexer.h" |
| 14 |
|
| 15 |
using namespace clang::ast_matchers; |
| 16 |
|
| 17 |
namespace clang::tidy::readability { |
| 18 |
|
| 19 |
static tok::TokenKind getTokenKind(SourceLocation Loc, const SourceManager &SM, |
| 20 |
const ASTContext *Context) { |
| 21 |
Token Tok; |
| 22 |
SourceLocation Beginning = |
| 23 |
Lexer::GetBeginningOfToken(Loc, SM, Context->getLangOpts()); |
| 24 |
const bool Invalid = |
| 25 |
Lexer::getRawToken(Beginning, Tok, SM, Context->getLangOpts()); |
| 26 |
assert(!Invalid && "Expected a valid token."); |
| 27 |
|
| 28 |
if (Invalid) |
| 29 |
return tok::NUM_TOKENS; |
| 30 |
|
| 31 |
return Tok.getKind(); |
| 32 |
} |
| 33 |
|
| 34 |
static SourceLocation |
| 35 |
forwardSkipWhitespaceAndComments(SourceLocation Loc, const SourceManager &SM, |
| 36 |
const ASTContext *Context) { |
| 37 |
assert(Loc.isValid()); |
| 38 |
for (;;) { |
| 39 |
while (isWhitespace(*SM.getCharacterData(Loc))) |
| 40 |
Loc = Loc.getLocWithOffset(1); |
| 41 |
|
| 42 |
tok::TokenKind TokKind = getTokenKind(Loc, SM, Context); |
| 43 |
if (TokKind != tok::comment) |
| 44 |
return Loc; |
| 45 |
|
| 46 |
// Fast-forward current token. |
| 47 |
Loc = Lexer::getLocForEndOfToken(Loc, 0, SM, Context->getLangOpts()); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
static SourceLocation findEndLocation(const Stmt &S, const SourceManager &SM, |
| 52 |
const ASTContext *Context) { |
| 53 |
SourceLocation Loc = |
| 54 |
utils::lexer::getUnifiedEndLoc(S, SM, Context->getLangOpts()); |
| 55 |
if (!Loc.isValid()) |
| 56 |
return Loc; |
| 57 |
|
| 58 |
// Start searching right after S. |
| 59 |
Loc = Loc.getLocWithOffset(1); |
| 60 |
|
| 61 |
for (;;) { |
| 62 |
assert(Loc.isValid()); |
| 63 |
while (isHorizontalWhitespace(*SM.getCharacterData(Loc))) { |
| 64 |
Loc = Loc.getLocWithOffset(1); |
| 65 |
} |
| 66 |
|
| 67 |
if (isVerticalWhitespace(*SM.getCharacterData(Loc))) { |
| 68 |
// EOL, insert brace before. |
| 69 |
break; |
| 70 |
} |
| 71 |
tok::TokenKind TokKind = getTokenKind(Loc, SM, Context); |
| 72 |
if (TokKind != tok::comment) { |
| 73 |
// Non-comment token, insert brace before. |
| 74 |
break; |
| 75 |
} |
| 76 |
|
| 77 |
SourceLocation TokEndLoc = |
| 78 |
Lexer::getLocForEndOfToken(Loc, 0, SM, Context->getLangOpts()); |
| 79 |
SourceRange TokRange(Loc, TokEndLoc); |
| 80 |
StringRef Comment = Lexer::getSourceText( |
| 81 |
CharSourceRange::getTokenRange(TokRange), SM, Context->getLangOpts()); |
| 82 |
if (Comment.startswith("/*") && Comment.contains('\n')) { |
| 83 |
// Multi-line block comment, insert brace before. |
| 84 |
break; |
| 85 |
} |
| 86 |
// else: Trailing comment, insert brace after the newline. |
| 87 |
|
| 88 |
// Fast-forward current token. |
| 89 |
Loc = TokEndLoc; |
| 90 |
} |
| 91 |
return Loc; |
| 92 |
} |
| 93 |
|
| 94 |
BracesAroundStatementsCheck::BracesAroundStatementsCheck( |
| 95 |
StringRef Name, ClangTidyContext *Context) |
| 96 |
: ClangTidyCheck(Name, Context), |
| 97 |
// Always add braces by default. |
| 98 |
ShortStatementLines(Options.get("ShortStatementLines", 0U)) {} |
| 99 |
|
| 100 |
void BracesAroundStatementsCheck::storeOptions( |
| 101 |
ClangTidyOptions::OptionMap &Opts) { |
| 102 |
Options.store(Opts, "ShortStatementLines", ShortStatementLines); |
| 103 |
} |
| 104 |
|
| 105 |
void BracesAroundStatementsCheck::registerMatchers(MatchFinder *Finder) { |
| 106 |
Finder->addMatcher(ifStmt().bind("if"), this); |
| 107 |
Finder->addMatcher(whileStmt().bind("while"), this); |
| 108 |
Finder->addMatcher(doStmt().bind("do"), this); |
| 109 |
Finder->addMatcher(forStmt().bind("for"), this); |
| 110 |
Finder->addMatcher(cxxForRangeStmt().bind("for-range"), this); |
| 111 |
} |
| 112 |
|
| 113 |
void BracesAroundStatementsCheck::check( |
| 114 |
const MatchFinder::MatchResult &Result) { |
| 115 |
const SourceManager &SM = *Result.SourceManager; |
| 116 |
const ASTContext *Context = Result.Context; |
| 117 |
|
| 118 |
// Get location of closing parenthesis or 'do' to insert opening brace. |
| 119 |
if (const auto *S = Result.Nodes.getNodeAs<ForStmt>("for")) { |
| 120 |
checkStmt(Result, S->getBody(), S->getRParenLoc()); |
| 121 |
} else if (const auto *S = |
| 122 |
Result.Nodes.getNodeAs<CXXForRangeStmt>("for-range")) { |
| 123 |
checkStmt(Result, S->getBody(), S->getRParenLoc()); |
| 124 |
} else if (const auto *S = Result.Nodes.getNodeAs<DoStmt>("do")) { |
| 125 |
checkStmt(Result, S->getBody(), S->getDoLoc(), S->getWhileLoc()); |
| 126 |
} else if (const auto *S = Result.Nodes.getNodeAs<WhileStmt>("while")) { |
| 127 |
SourceLocation StartLoc = findRParenLoc(S, SM, Context); |
| 128 |
if (StartLoc.isInvalid()) |
| 129 |
return; |
| 130 |
checkStmt(Result, S->getBody(), StartLoc); |
| 131 |
} else if (const auto *S = Result.Nodes.getNodeAs<IfStmt>("if")) { |
| 132 |
// "if consteval" always has braces. |
| 133 |
if (S->isConsteval()) |
| 134 |
return; |
| 135 |
|
| 136 |
SourceLocation StartLoc = findRParenLoc(S, SM, Context); |
| 137 |
if (StartLoc.isInvalid()) |
| 138 |
return; |
| 139 |
if (ForceBracesStmts.erase(S)) |
| 140 |
ForceBracesStmts.insert(S->getThen()); |
| 141 |
bool BracedIf = checkStmt(Result, S->getThen(), StartLoc, S->getElseLoc()); |
| 142 |
const Stmt *Else = S->getElse(); |
| 143 |
if (Else && BracedIf) |
| 144 |
ForceBracesStmts.insert(Else); |
| 145 |
if (Else && !isa<IfStmt>(Else)) { |
| 146 |
// Omit 'else if' statements here, they will be handled directly. |
| 147 |
checkStmt(Result, Else, S->getElseLoc()); |
| 148 |
} |
| 149 |
} else { |
| 150 |
llvm_unreachable("Invalid match"); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
/// Find location of right parenthesis closing condition. |
| 155 |
template <typename IfOrWhileStmt> |
| 156 |
SourceLocation |
| 157 |
BracesAroundStatementsCheck::findRParenLoc(const IfOrWhileStmt *S, |
| 158 |
const SourceManager &SM, |
| 159 |
const ASTContext *Context) { |
| 160 |
// Skip macros. |
| 161 |
if (S->getBeginLoc().isMacroID()) |
| 162 |
return SourceLocation(); |
| 163 |
|
| 164 |
SourceLocation CondEndLoc = S->getCond()->getEndLoc(); |
| 165 |
if (const DeclStmt *CondVar = S->getConditionVariableDeclStmt()) |
| 166 |
CondEndLoc = CondVar->getEndLoc(); |
| 167 |
|
| 168 |
if (!CondEndLoc.isValid()) { |
| 169 |
return SourceLocation(); |
| 170 |
} |
| 171 |
|
| 172 |
SourceLocation PastCondEndLoc = |
| 173 |
Lexer::getLocForEndOfToken(CondEndLoc, 0, SM, Context->getLangOpts()); |
| 174 |
if (PastCondEndLoc.isInvalid()) |
| 175 |
return SourceLocation(); |
| 176 |
SourceLocation RParenLoc = |
| 177 |
forwardSkipWhitespaceAndComments(PastCondEndLoc, SM, Context); |
| 178 |
if (RParenLoc.isInvalid()) |
| 179 |
return SourceLocation(); |
| 180 |
tok::TokenKind TokKind = getTokenKind(RParenLoc, SM, Context); |
| 181 |
if (TokKind != tok::r_paren) |
| 182 |
return SourceLocation(); |
| 183 |
return RParenLoc; |
| 184 |
} |
| 185 |
|
| 186 |
/// Determine if the statement needs braces around it, and add them if it does. |
| 187 |
/// Returns true if braces where added. |
| 188 |
bool BracesAroundStatementsCheck::checkStmt( |
| 189 |
const MatchFinder::MatchResult &Result, const Stmt *S, |
| 190 |
SourceLocation InitialLoc, SourceLocation EndLocHint) { |
| 191 |
|
| 192 |
while (const auto *AS = dyn_cast<AttributedStmt>(S)) |
| 193 |
S = AS->getSubStmt(); |
| 194 |
|
| 195 |
// 1) If there's a corresponding "else" or "while", the check inserts "} " |
| 196 |
// right before that token. |
| 197 |
// 2) If there's a multi-line block comment starting on the same line after |
| 198 |
// the location we're inserting the closing brace at, or there's a non-comment |
| 199 |
// token, the check inserts "\n}" right before that token. |
| 200 |
// 3) Otherwise the check finds the end of line (possibly after some block or |
| 201 |
// line comments) and inserts "\n}" right before that EOL. |
| 202 |
if (!S || isa<CompoundStmt>(S)) { |
| 203 |
// Already inside braces. |
| 204 |
return false; |
| 205 |
} |
| 206 |
|
| 207 |
if (!InitialLoc.isValid()) |
| 208 |
return false; |
| 209 |
const SourceManager &SM = *Result.SourceManager; |
| 210 |
const ASTContext *Context = Result.Context; |
| 211 |
|
| 212 |
// Convert InitialLoc to file location, if it's on the same macro expansion |
| 213 |
// level as the start of the statement. We also need file locations for |
| 214 |
// Lexer::getLocForEndOfToken working properly. |
| 215 |
InitialLoc = Lexer::makeFileCharRange( |
| 216 |
CharSourceRange::getCharRange(InitialLoc, S->getBeginLoc()), |
| 217 |
SM, Context->getLangOpts()) |
| 218 |
.getBegin(); |
| 219 |
if (InitialLoc.isInvalid()) |
| 220 |
return false; |
| 221 |
SourceLocation StartLoc = |
| 222 |
Lexer::getLocForEndOfToken(InitialLoc, 0, SM, Context->getLangOpts()); |
| 223 |
|
| 224 |
// StartLoc points at the location of the opening brace to be inserted. |
| 225 |
SourceLocation EndLoc; |
| 226 |
std::string ClosingInsertion; |
| 227 |
if (EndLocHint.isValid()) { |
| 228 |
EndLoc = EndLocHint; |
| 229 |
ClosingInsertion = "} "; |
| 230 |
} else { |
| 231 |
EndLoc = findEndLocation(*S, SM, Context); |
| 232 |
ClosingInsertion = "\n}"; |
| 233 |
} |
| 234 |
|
| 235 |
assert(StartLoc.isValid()); |
| 236 |
|
| 237 |
// Don't require braces for statements spanning less than certain number of |
| 238 |
// lines. |
| 239 |
if (ShortStatementLines && !ForceBracesStmts.erase(S)) { |
| 240 |
unsigned StartLine = SM.getSpellingLineNumber(StartLoc); |
| 241 |
unsigned EndLine = SM.getSpellingLineNumber(EndLoc); |
| 242 |
if (EndLine - StartLine < ShortStatementLines) |
| 243 |
return false; |
| 244 |
} |
| 245 |
|
| 246 |
auto Diag = diag(StartLoc, "statement should be inside braces"); |
| 247 |
|
| 248 |
// Change only if StartLoc and EndLoc are on the same macro expansion level. |
| 249 |
// This will also catch invalid EndLoc. |
| 250 |
// Example: LLVM_DEBUG( for(...) do_something() ); |
| 251 |
// In this case fix-it cannot be provided as the semicolon which is not |
| 252 |
// visible here is part of the macro. Adding braces here would require adding |
| 253 |
// another semicolon. |
| 254 |
if (Lexer::makeFileCharRange( |
| 255 |
CharSourceRange::getTokenRange(SourceRange( |
| 256 |
SM.getSpellingLoc(StartLoc), SM.getSpellingLoc(EndLoc))), |
| 257 |
SM, Context->getLangOpts()) |
| 258 |
.isInvalid()) |
| 259 |
return false; |
| 260 |
|
| 261 |
Diag << FixItHint::CreateInsertion(StartLoc, " {") |
| 262 |
<< FixItHint::CreateInsertion(EndLoc, ClosingInsertion); |
| 263 |
return true; |
| 264 |
} |
| 265 |
|
| 266 |
void BracesAroundStatementsCheck::onEndOfTranslationUnit() { |
| 267 |
ForceBracesStmts.clear(); |
| 268 |
} |
| 269 |
|
| 270 |
} // namespace clang::tidy::readability |