Gabor Horvath | 8b6434e | 2016-02-11 09:23:33 +0000 | [diff] [blame] | 1 | //===--- 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 | |
Gabor Horvath | 8b6434e | 2016-02-11 09:23:33 +0000 | [diff] [blame] | 10 | #include "SuspiciousSemicolonCheck.h" |
Gabor Horvath | f4336ac | 2016-03-03 13:08:11 +0000 | [diff] [blame] | 11 | #include "../utils/LexerUtils.h" |
Gabor Horvath | 8b6434e | 2016-02-11 09:23:33 +0000 | [diff] [blame] | 12 | #include "clang/AST/ASTContext.h" |
| 13 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 14 | |
| 15 | using namespace clang::ast_matchers; |
| 16 | |
| 17 | namespace clang { |
| 18 | namespace tidy { |
| 19 | namespace misc { |
| 20 | |
| 21 | void 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 | |
| 32 | void SuspiciousSemicolonCheck::check(const MatchFinder::MatchResult &Result) { |
Alexander Kornienko | 385c2a3 | 2017-02-08 16:11:22 +0000 | [diff] [blame] | 33 | if (Result.Context->getDiagnostics().hasUncompilableErrorOccurred()) |
Gabor Horvath | f4336ac | 2016-03-03 13:08:11 +0000 | [diff] [blame] | 34 | return; |
| 35 | |
Gabor Horvath | 8b6434e | 2016-02-11 09:23:33 +0000 | [diff] [blame] | 36 | const auto *Semicolon = Result.Nodes.getNodeAs<NullStmt>("semi"); |
| 37 | SourceLocation LocStart = Semicolon->getLocStart(); |
| 38 | |
| 39 | if (LocStart.isMacroID()) |
| 40 | return; |
| 41 | |
| 42 | ASTContext &Ctxt = *Result.Context; |
Alexander Kornienko | 2d73022 | 2017-02-06 15:46:33 +0000 | [diff] [blame] | 43 | auto Token = utils::lexer::getPreviousToken(Ctxt, LocStart); |
Gabor Horvath | 8b6434e | 2016-02-11 09:23:33 +0000 | [diff] [blame] | 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->getLocEnd(); |
| 55 | FileID FID = SM.getFileID(LocEnd); |
| 56 | 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->getLocStart()); |
| 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 misc |
| 76 | } // namespace tidy |
| 77 | } // namespace clang |