2014-10-03 03:09:56 +08:00
|
|
|
//===--- BracesAroundStatementsCheck.cpp - clang-tidy ---------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "BracesAroundStatementsCheck.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchers.h"
|
|
|
|
#include "clang/Lex/Lexer.h"
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
2014-10-15 18:51:57 +08:00
|
|
|
namespace readability {
|
2014-10-03 03:09:56 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
tok::TokenKind getTokenKind(SourceLocation Loc, const SourceManager &SM,
|
|
|
|
const ASTContext *Context) {
|
|
|
|
Token Tok;
|
|
|
|
SourceLocation Beginning =
|
|
|
|
Lexer::GetBeginningOfToken(Loc, SM, Context->getLangOpts());
|
|
|
|
const bool Invalid =
|
|
|
|
Lexer::getRawToken(Beginning, Tok, SM, Context->getLangOpts());
|
|
|
|
assert(!Invalid && "Expected a valid token.");
|
|
|
|
|
|
|
|
if (Invalid)
|
|
|
|
return tok::NUM_TOKENS;
|
|
|
|
|
|
|
|
return Tok.getKind();
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceLocation forwardSkipWhitespaceAndComments(SourceLocation Loc,
|
|
|
|
const SourceManager &SM,
|
|
|
|
const ASTContext *Context) {
|
|
|
|
assert(Loc.isValid());
|
|
|
|
for (;;) {
|
|
|
|
while (isWhitespace(*FullSourceLoc(Loc, SM).getCharacterData()))
|
|
|
|
Loc = Loc.getLocWithOffset(1);
|
|
|
|
|
|
|
|
tok::TokenKind TokKind = getTokenKind(Loc, SM, Context);
|
|
|
|
if (TokKind == tok::NUM_TOKENS || TokKind != tok::comment)
|
|
|
|
return Loc;
|
|
|
|
|
|
|
|
// Fast-forward current token.
|
|
|
|
Loc = Lexer::getLocForEndOfToken(Loc, 0, SM, Context->getLangOpts());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceLocation findEndLocation(SourceLocation LastTokenLoc,
|
|
|
|
const SourceManager &SM,
|
|
|
|
const ASTContext *Context) {
|
|
|
|
SourceLocation Loc = LastTokenLoc;
|
|
|
|
// Loc points to the beginning of the last (non-comment non-ws) token
|
|
|
|
// before end or ';'.
|
|
|
|
assert(Loc.isValid());
|
|
|
|
bool SkipEndWhitespaceAndComments = true;
|
|
|
|
tok::TokenKind TokKind = getTokenKind(Loc, SM, Context);
|
|
|
|
if (TokKind == tok::NUM_TOKENS || TokKind == tok::semi ||
|
|
|
|
TokKind == tok::r_brace) {
|
|
|
|
// If we are at ";" or "}", we found the last token. We could use as well
|
|
|
|
// `if (isa<NullStmt>(S))`, but it wouldn't work for nested statements.
|
|
|
|
SkipEndWhitespaceAndComments = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Loc = Lexer::getLocForEndOfToken(Loc, 0, SM, Context->getLangOpts());
|
|
|
|
// Loc points past the last token before end or after ';'.
|
|
|
|
|
|
|
|
if (SkipEndWhitespaceAndComments) {
|
|
|
|
Loc = forwardSkipWhitespaceAndComments(Loc, SM, Context);
|
|
|
|
tok::TokenKind TokKind = getTokenKind(Loc, SM, Context);
|
|
|
|
if (TokKind == tok::semi)
|
|
|
|
Loc = Lexer::getLocForEndOfToken(Loc, 0, SM, Context->getLangOpts());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
assert(Loc.isValid());
|
|
|
|
while (isHorizontalWhitespace(*FullSourceLoc(Loc, SM).getCharacterData()))
|
|
|
|
Loc = Loc.getLocWithOffset(1);
|
|
|
|
|
|
|
|
if (isVerticalWhitespace(*FullSourceLoc(Loc, SM).getCharacterData())) {
|
|
|
|
// EOL, insert brace before.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tok::TokenKind TokKind = getTokenKind(Loc, SM, Context);
|
|
|
|
if (TokKind != tok::comment) {
|
|
|
|
// Non-comment token, insert brace before.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceLocation TokEndLoc =
|
|
|
|
Lexer::getLocForEndOfToken(Loc, 0, SM, Context->getLangOpts());
|
|
|
|
SourceRange TokRange(Loc, TokEndLoc);
|
|
|
|
StringRef Comment = Lexer::getSourceText(
|
|
|
|
CharSourceRange::getTokenRange(TokRange), SM, Context->getLangOpts());
|
|
|
|
if (Comment.startswith("/*") && Comment.find('\n') != StringRef::npos) {
|
|
|
|
// Multi-line block comment, insert brace before.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// else: Trailing comment, insert brace after the newline.
|
|
|
|
|
|
|
|
// Fast-forward current token.
|
|
|
|
Loc = TokEndLoc;
|
|
|
|
}
|
|
|
|
return Loc;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2014-10-13 20:46:22 +08:00
|
|
|
BracesAroundStatementsCheck::BracesAroundStatementsCheck(
|
|
|
|
StringRef Name, ClangTidyContext *Context)
|
|
|
|
: ClangTidyCheck(Name, Context),
|
|
|
|
// Always add braces by default.
|
|
|
|
ShortStatementLines(Options.get("ShortStatementLines", 0U)) {}
|
|
|
|
|
|
|
|
void
|
|
|
|
BracesAroundStatementsCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
|
|
|
|
Options.store(Opts, "ShortStatementLines", ShortStatementLines);
|
|
|
|
}
|
|
|
|
|
2014-10-03 03:09:56 +08:00
|
|
|
void BracesAroundStatementsCheck::registerMatchers(MatchFinder *Finder) {
|
|
|
|
Finder->addMatcher(ifStmt().bind("if"), this);
|
|
|
|
Finder->addMatcher(whileStmt().bind("while"), this);
|
|
|
|
Finder->addMatcher(doStmt().bind("do"), this);
|
|
|
|
Finder->addMatcher(forStmt().bind("for"), this);
|
2015-09-17 21:31:25 +08:00
|
|
|
Finder->addMatcher(cxxForRangeStmt().bind("for-range"), this);
|
2014-10-03 03:09:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BracesAroundStatementsCheck::check(const MatchFinder::MatchResult &Result) {
|
|
|
|
const SourceManager &SM = *Result.SourceManager;
|
|
|
|
const ASTContext *Context = Result.Context;
|
|
|
|
|
|
|
|
// Get location of closing parenthesis or 'do' to insert opening brace.
|
|
|
|
if (auto S = Result.Nodes.getNodeAs<ForStmt>("for")) {
|
|
|
|
checkStmt(Result, S->getBody(), S->getRParenLoc());
|
|
|
|
} else if (auto S = Result.Nodes.getNodeAs<CXXForRangeStmt>("for-range")) {
|
|
|
|
checkStmt(Result, S->getBody(), S->getRParenLoc());
|
|
|
|
} else if (auto S = Result.Nodes.getNodeAs<DoStmt>("do")) {
|
|
|
|
checkStmt(Result, S->getBody(), S->getDoLoc(), S->getWhileLoc());
|
|
|
|
} else if (auto S = Result.Nodes.getNodeAs<WhileStmt>("while")) {
|
|
|
|
SourceLocation StartLoc = findRParenLoc(S, SM, Context);
|
|
|
|
if (StartLoc.isInvalid())
|
|
|
|
return;
|
|
|
|
checkStmt(Result, S->getBody(), StartLoc);
|
|
|
|
} else if (auto S = Result.Nodes.getNodeAs<IfStmt>("if")) {
|
|
|
|
SourceLocation StartLoc = findRParenLoc(S, SM, Context);
|
|
|
|
if (StartLoc.isInvalid())
|
|
|
|
return;
|
2015-06-05 00:36:58 +08:00
|
|
|
if (ForceBracesStmts.erase(S))
|
|
|
|
ForceBracesStmts.insert(S->getThen());
|
2015-03-31 21:53:03 +08:00
|
|
|
bool BracedIf = checkStmt(Result, S->getThen(), StartLoc, S->getElseLoc());
|
2014-10-03 03:09:56 +08:00
|
|
|
const Stmt *Else = S->getElse();
|
2015-06-05 00:36:58 +08:00
|
|
|
if (Else && BracedIf)
|
|
|
|
ForceBracesStmts.insert(Else);
|
2014-10-03 03:09:56 +08:00
|
|
|
if (Else && !isa<IfStmt>(Else)) {
|
|
|
|
// Omit 'else if' statements here, they will be handled directly.
|
2015-06-05 00:36:58 +08:00
|
|
|
checkStmt(Result, Else, S->getElseLoc(), SourceLocation());
|
2014-10-03 03:09:56 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
llvm_unreachable("Invalid match");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Find location of right parenthesis closing condition
|
|
|
|
template <typename IfOrWhileStmt>
|
|
|
|
SourceLocation
|
|
|
|
BracesAroundStatementsCheck::findRParenLoc(const IfOrWhileStmt *S,
|
|
|
|
const SourceManager &SM,
|
|
|
|
const ASTContext *Context) {
|
2015-09-10 01:06:09 +08:00
|
|
|
// Skip macros.
|
2014-10-03 03:09:56 +08:00
|
|
|
if (S->getLocStart().isMacroID())
|
|
|
|
return SourceLocation();
|
|
|
|
|
|
|
|
SourceLocation CondEndLoc = S->getCond()->getLocEnd();
|
|
|
|
if (const DeclStmt *CondVar = S->getConditionVariableDeclStmt())
|
|
|
|
CondEndLoc = CondVar->getLocEnd();
|
|
|
|
|
2016-02-11 17:57:55 +08:00
|
|
|
if (!CondEndLoc.isValid()) {
|
|
|
|
return SourceLocation();
|
|
|
|
}
|
|
|
|
|
2014-10-03 03:09:56 +08:00
|
|
|
SourceLocation PastCondEndLoc =
|
|
|
|
Lexer::getLocForEndOfToken(CondEndLoc, 0, SM, Context->getLangOpts());
|
2015-12-16 21:19:08 +08:00
|
|
|
if (PastCondEndLoc.isInvalid())
|
2014-10-03 03:09:56 +08:00
|
|
|
return SourceLocation();
|
|
|
|
SourceLocation RParenLoc =
|
|
|
|
forwardSkipWhitespaceAndComments(PastCondEndLoc, SM, Context);
|
2015-12-16 21:19:08 +08:00
|
|
|
if (RParenLoc.isInvalid())
|
2014-10-03 03:09:56 +08:00
|
|
|
return SourceLocation();
|
|
|
|
tok::TokenKind TokKind = getTokenKind(RParenLoc, SM, Context);
|
2015-12-16 21:19:08 +08:00
|
|
|
if (TokKind != tok::r_paren)
|
2014-10-03 03:09:56 +08:00
|
|
|
return SourceLocation();
|
|
|
|
return RParenLoc;
|
|
|
|
}
|
|
|
|
|
2015-03-31 21:53:03 +08:00
|
|
|
/// Determine if the statement needs braces around it, and add them if it does.
|
|
|
|
/// Returns true if braces where added.
|
|
|
|
bool BracesAroundStatementsCheck::checkStmt(
|
|
|
|
const MatchFinder::MatchResult &Result, const Stmt *S,
|
2015-06-05 00:36:58 +08:00
|
|
|
SourceLocation InitialLoc, SourceLocation EndLocHint) {
|
2014-10-03 03:09:56 +08:00
|
|
|
// 1) If there's a corresponding "else" or "while", the check inserts "} "
|
|
|
|
// right before that token.
|
|
|
|
// 2) If there's a multi-line block comment starting on the same line after
|
|
|
|
// the location we're inserting the closing brace at, or there's a non-comment
|
|
|
|
// token, the check inserts "\n}" right before that token.
|
|
|
|
// 3) Otherwise the check finds the end of line (possibly after some block or
|
|
|
|
// line comments) and inserts "\n}" right before that EOL.
|
|
|
|
if (!S || isa<CompoundStmt>(S)) {
|
|
|
|
// Already inside braces.
|
2015-03-31 21:53:03 +08:00
|
|
|
return false;
|
2014-10-03 03:09:56 +08:00
|
|
|
}
|
|
|
|
|
2016-02-16 18:31:33 +08:00
|
|
|
if (!InitialLoc.isValid())
|
|
|
|
return false;
|
2014-10-03 03:09:56 +08:00
|
|
|
const SourceManager &SM = *Result.SourceManager;
|
|
|
|
const ASTContext *Context = Result.Context;
|
|
|
|
|
2015-09-10 01:06:09 +08:00
|
|
|
// Treat macros.
|
|
|
|
CharSourceRange FileRange = Lexer::makeFileCharRange(
|
|
|
|
CharSourceRange::getTokenRange(S->getSourceRange()), SM,
|
|
|
|
Context->getLangOpts());
|
|
|
|
if (FileRange.isInvalid())
|
|
|
|
return false;
|
|
|
|
|
2015-09-30 20:48:42 +08:00
|
|
|
// Convert InitialLoc to file location, if it's on the same macro expansion
|
|
|
|
// level as the start of the statement. We also need file locations for
|
|
|
|
// Lexer::getLocForEndOfToken working properly.
|
|
|
|
InitialLoc = Lexer::makeFileCharRange(
|
|
|
|
CharSourceRange::getCharRange(InitialLoc, S->getLocStart()),
|
|
|
|
SM, Context->getLangOpts())
|
|
|
|
.getBegin();
|
|
|
|
if (InitialLoc.isInvalid())
|
|
|
|
return false;
|
2014-10-03 03:09:56 +08:00
|
|
|
SourceLocation StartLoc =
|
|
|
|
Lexer::getLocForEndOfToken(InitialLoc, 0, SM, Context->getLangOpts());
|
2015-09-30 20:48:42 +08:00
|
|
|
|
2014-10-03 03:09:56 +08:00
|
|
|
// StartLoc points at the location of the opening brace to be inserted.
|
|
|
|
SourceLocation EndLoc;
|
|
|
|
std::string ClosingInsertion;
|
|
|
|
if (EndLocHint.isValid()) {
|
|
|
|
EndLoc = EndLocHint;
|
|
|
|
ClosingInsertion = "} ";
|
|
|
|
} else {
|
2015-09-10 01:06:09 +08:00
|
|
|
const auto FREnd = FileRange.getEnd().getLocWithOffset(-1);
|
|
|
|
EndLoc = findEndLocation(FREnd, SM, Context);
|
2014-10-03 03:09:56 +08:00
|
|
|
ClosingInsertion = "\n}";
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(StartLoc.isValid());
|
2014-10-13 20:46:22 +08:00
|
|
|
assert(EndLoc.isValid());
|
|
|
|
// Don't require braces for statements spanning less than certain number of
|
|
|
|
// lines.
|
2015-06-05 00:36:58 +08:00
|
|
|
if (ShortStatementLines && !ForceBracesStmts.erase(S)) {
|
2014-10-13 20:46:22 +08:00
|
|
|
unsigned StartLine = SM.getSpellingLineNumber(StartLoc);
|
|
|
|
unsigned EndLine = SM.getSpellingLineNumber(EndLoc);
|
|
|
|
if (EndLine - StartLine < ShortStatementLines)
|
2015-03-31 21:53:03 +08:00
|
|
|
return false;
|
2014-10-13 20:46:22 +08:00
|
|
|
}
|
|
|
|
|
2014-10-03 03:09:56 +08:00
|
|
|
auto Diag = diag(StartLoc, "statement should be inside braces");
|
|
|
|
Diag << FixItHint::CreateInsertion(StartLoc, " {")
|
|
|
|
<< FixItHint::CreateInsertion(EndLoc, ClosingInsertion);
|
2015-03-31 21:53:03 +08:00
|
|
|
return true;
|
2014-10-03 03:09:56 +08:00
|
|
|
}
|
|
|
|
|
2015-06-05 00:36:58 +08:00
|
|
|
void BracesAroundStatementsCheck::onEndOfTranslationUnit() {
|
|
|
|
ForceBracesStmts.clear();
|
|
|
|
}
|
|
|
|
|
2014-10-15 18:51:57 +08:00
|
|
|
} // namespace readability
|
2014-10-03 03:09:56 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|