20 stmt(anyOf(ifStmt(hasThen(nullStmt().bind(
"semi")),
21 unless(hasElse(stmt())), unless(isConstexpr())),
22 forStmt(hasBody(nullStmt().bind(
"semi"))),
23 cxxForRangeStmt(hasBody(nullStmt().bind(
"semi"))),
24 whileStmt(hasBody(nullStmt().bind(
"semi")))))
30 if (Result.Context->getDiagnostics().hasUncompilableErrorOccurred())
33 const auto *Semicolon = Result.Nodes.getNodeAs<NullStmt>(
"semi");
34 SourceLocation LocStart = Semicolon->getBeginLoc();
36 if (LocStart.isMacroID())
39 ASTContext &Ctxt = *Result.Context;
42 auto &SM = *Result.SourceManager;
43 unsigned SemicolonLine = SM.getSpellingLineNumber(LocStart);
45 const auto *Statement = Result.Nodes.getNodeAs<Stmt>(
"stmt");
46 const bool IsIfStmt = isa<IfStmt>(Statement);
49 SM.getSpellingLineNumber(Token.getLocation()) != SemicolonLine)
52 SourceLocation LocEnd = Semicolon->getEndLoc();
53 FileID FID = SM.getFileID(LocEnd);
54 llvm::MemoryBufferRef Buffer = SM.getBufferOrFake(FID, LocEnd);
55 Lexer Lexer(SM.getLocForStartOfFile(FID), Ctxt.getLangOpts(),
56 Buffer.getBufferStart(), SM.getCharacterData(LocEnd) + 1,
57 Buffer.getBufferEnd());
58 if (Lexer.LexFromRawLexer(Token))
61 unsigned BaseIndent = SM.getSpellingColumnNumber(Statement->getBeginLoc());
62 unsigned NewTokenIndent = SM.getSpellingColumnNumber(Token.getLocation());
63 unsigned NewTokenLine = SM.getSpellingLineNumber(Token.getLocation());
65 if (!IsIfStmt && NewTokenIndent <= BaseIndent &&
66 Token.getKind() != tok::l_brace && NewTokenLine != SemicolonLine)
69 diag(LocStart,
"potentially unintended semicolon")
70 << FixItHint::CreateRemoval(SourceRange(LocStart, LocEnd));
Token getPreviousToken(SourceLocation Location, const SourceManager &SM, const LangOptions &LangOpts, bool SkipComments)
Returns previous token or tok::unknown if not found.