blob: 5330f70e6ae867ca19a8dcb16add0c1f58a60479 [file] [log] [blame]
Gabor Horvath8b6434e2016-02-11 09:23:33 +00001//===--- SuspiciousSemicolonCheck.cpp - clang-tidy-------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "../utils/LexerUtils.h"
11#include "SuspiciousSemicolonCheck.h"
12#include "clang/AST/ASTContext.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
14
15using namespace clang::ast_matchers;
16
17namespace clang {
18namespace tidy {
19namespace misc {
20
21void SuspiciousSemicolonCheck::registerMatchers(MatchFinder *Finder) {
22 Finder->addMatcher(
23 stmt(anyOf(ifStmt(hasThen(nullStmt().bind("semi")),
24 unless(hasElse(stmt()))),
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
32void SuspiciousSemicolonCheck::check(const MatchFinder::MatchResult &Result) {
33 const auto *Semicolon = Result.Nodes.getNodeAs<NullStmt>("semi");
34 SourceLocation LocStart = Semicolon->getLocStart();
35
36 if (LocStart.isMacroID())
37 return;
38
39 ASTContext &Ctxt = *Result.Context;
40 auto Token = lexer_utils::getPreviousNonCommentToken(Ctxt, LocStart);
41 auto &SM = *Result.SourceManager;
42 unsigned SemicolonLine = SM.getSpellingLineNumber(LocStart);
43
44 const auto *Statement = Result.Nodes.getNodeAs<Stmt>("stmt");
45 const bool IsIfStmt = isa<IfStmt>(Statement);
46
47 if (!IsIfStmt &&
48 SM.getSpellingLineNumber(Token.getLocation()) != SemicolonLine)
49 return;
50
51 SourceLocation LocEnd = Semicolon->getLocEnd();
52 FileID FID = SM.getFileID(LocEnd);
53 llvm::MemoryBuffer *Buffer = SM.getBuffer(FID, LocEnd);
54 Lexer Lexer(SM.getLocForStartOfFile(FID), Ctxt.getLangOpts(),
55 Buffer->getBufferStart(), SM.getCharacterData(LocEnd) + 1,
56 Buffer->getBufferEnd());
57 if (Lexer.LexFromRawLexer(Token))
58 return;
59
60 unsigned BaseIndent = SM.getSpellingColumnNumber(Statement->getLocStart());
61 unsigned NewTokenIndent = SM.getSpellingColumnNumber(Token.getLocation());
62 unsigned NewTokenLine = SM.getSpellingLineNumber(Token.getLocation());
63
64 if (!IsIfStmt && NewTokenIndent <= BaseIndent &&
65 Token.getKind() != tok::l_brace && NewTokenLine != SemicolonLine)
66 return;
67
68 diag(LocStart, "potentially unintended semicolon")
69 << FixItHint::CreateRemoval(SourceRange(LocStart, LocEnd));
70}
71
72} // namespace misc
73} // namespace tidy
74} // namespace clang