clang-tools  9.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 
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  forStmt(hasBody(nullStmt().bind("semi"))),
25  cxxForRangeStmt(hasBody(nullStmt().bind("semi"))),
26  whileStmt(hasBody(nullStmt().bind("semi")))))
27  .bind("stmt"),
28  this);
29 }
30 
31 void 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  SourceLocation LocStart = Semicolon->getBeginLoc();
37 
38  if (LocStart.isMacroID())
39  return;
40 
41  ASTContext &Ctxt = *Result.Context;
42  auto Token = utils::lexer::getPreviousToken(LocStart, Ctxt.getSourceManager(),
43  Ctxt.getLangOpts());
44  auto &SM = *Result.SourceManager;
45  unsigned SemicolonLine = SM.getSpellingLineNumber(LocStart);
46 
47  const auto *Statement = Result.Nodes.getNodeAs<Stmt>("stmt");
48  const bool IsIfStmt = isa<IfStmt>(Statement);
49 
50  if (!IsIfStmt &&
51  SM.getSpellingLineNumber(Token.getLocation()) != SemicolonLine)
52  return;
53 
54  SourceLocation LocEnd = Semicolon->getEndLoc();
55  FileID FID = SM.getFileID(LocEnd);
56  const llvm::MemoryBuffer *Buffer = SM.getBuffer(FID, LocEnd);
57  Lexer Lexer(SM.getLocForStartOfFile(FID), Ctxt.getLangOpts(),
58  Buffer->getBufferStart(), SM.getCharacterData(LocEnd) + 1,
59  Buffer->getBufferEnd());
60  if (Lexer.LexFromRawLexer(Token))
61  return;
62 
63  unsigned BaseIndent = SM.getSpellingColumnNumber(Statement->getBeginLoc());
64  unsigned NewTokenIndent = SM.getSpellingColumnNumber(Token.getLocation());
65  unsigned NewTokenLine = SM.getSpellingLineNumber(Token.getLocation());
66 
67  if (!IsIfStmt && NewTokenIndent <= BaseIndent &&
68  Token.getKind() != tok::l_brace && NewTokenLine != SemicolonLine)
69  return;
70 
71  diag(LocStart, "potentially unintended semicolon")
72  << FixItHint::CreateRemoval(SourceRange(LocStart, LocEnd));
73 }
74 
75 } // namespace bugprone
76 } // namespace tidy
77 } // namespace clang
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
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
Definition: Rename.cpp:36