clang-tools 10.0.0

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