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