1 | //===--- SuspiciousSemicolonCheck.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 "SuspiciousSemicolonCheck.h" |
10 | #include "../utils/LexerUtils.h" |
11 | #include "clang/AST/ASTContext.h" |
12 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
13 | |
14 | using namespace clang::ast_matchers; |
15 | |
16 | namespace clang::tidy::bugprone { |
17 | |
18 | void SuspiciousSemicolonCheck::registerMatchers(MatchFinder *Finder) { |
19 | Finder->addMatcher( |
20 | NodeMatch: stmt(anyOf(ifStmt(hasThen(InnerMatcher: nullStmt().bind(ID: "semi" )), |
21 | unless(hasElse(InnerMatcher: stmt())), unless(isConstexpr())), |
22 | forStmt(hasBody(InnerMatcher: nullStmt().bind(ID: "semi" ))), |
23 | cxxForRangeStmt(hasBody(InnerMatcher: nullStmt().bind(ID: "semi" ))), |
24 | whileStmt(hasBody(InnerMatcher: nullStmt().bind(ID: "semi" ))))) |
25 | .bind(ID: "stmt" ), |
26 | Action: this); |
27 | } |
28 | |
29 | void SuspiciousSemicolonCheck::check(const MatchFinder::MatchResult &Result) { |
30 | if (Result.Context->getDiagnostics().hasUncompilableErrorOccurred()) |
31 | return; |
32 | |
33 | const auto *Semicolon = Result.Nodes.getNodeAs<NullStmt>(ID: "semi" ); |
34 | SourceLocation LocStart = Semicolon->getBeginLoc(); |
35 | |
36 | if (LocStart.isMacroID()) |
37 | return; |
38 | |
39 | ASTContext &Ctxt = *Result.Context; |
40 | auto Token = utils::lexer::getPreviousToken(Location: LocStart, SM: Ctxt.getSourceManager(), |
41 | LangOpts: Ctxt.getLangOpts()); |
42 | auto &SM = *Result.SourceManager; |
43 | unsigned SemicolonLine = SM.getSpellingLineNumber(Loc: LocStart); |
44 | |
45 | const auto *Statement = Result.Nodes.getNodeAs<Stmt>(ID: "stmt" ); |
46 | const bool IsIfStmt = isa<IfStmt>(Val: Statement); |
47 | |
48 | if (!IsIfStmt && |
49 | SM.getSpellingLineNumber(Loc: Token.getLocation()) != SemicolonLine) |
50 | return; |
51 | |
52 | SourceLocation LocEnd = Semicolon->getEndLoc(); |
53 | FileID FID = SM.getFileID(SpellingLoc: LocEnd); |
54 | llvm::MemoryBufferRef Buffer = SM.getBufferOrFake(FID, Loc: LocEnd); |
55 | Lexer Lexer(SM.getLocForStartOfFile(FID), Ctxt.getLangOpts(), |
56 | Buffer.getBufferStart(), SM.getCharacterData(SL: LocEnd) + 1, |
57 | Buffer.getBufferEnd()); |
58 | if (Lexer.LexFromRawLexer(Result&: Token)) |
59 | return; |
60 | |
61 | unsigned BaseIndent = SM.getSpellingColumnNumber(Loc: Statement->getBeginLoc()); |
62 | unsigned NewTokenIndent = SM.getSpellingColumnNumber(Loc: Token.getLocation()); |
63 | unsigned NewTokenLine = SM.getSpellingLineNumber(Loc: Token.getLocation()); |
64 | |
65 | if (!IsIfStmt && NewTokenIndent <= BaseIndent && |
66 | Token.getKind() != tok::l_brace && NewTokenLine != SemicolonLine) |
67 | return; |
68 | |
69 | diag(Loc: LocStart, Description: "potentially unintended semicolon" ) |
70 | << FixItHint::CreateRemoval(RemoveRange: SourceRange(LocStart, LocEnd)); |
71 | } |
72 | |
73 | } // namespace clang::tidy::bugprone |
74 | |