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 const SourceLocation LocStart = Semicolon->getBeginLoc();
36 if (LocStart.isMacroID())
39 ASTContext &Ctxt = *Result.Context;
40 const auto &SM = *Result.SourceManager;
41 const unsigned SemicolonLine = SM.getSpellingLineNumber(LocStart);
43 const auto *Statement = Result.Nodes.getNodeAs<Stmt>(
"stmt");
44 const bool IsIfStmt = isa<IfStmt>(Statement);
47 LocStart, Ctxt.getSourceManager(), Ctxt.getLangOpts());
48 if (!PrevTok || (!IsIfStmt && SM.getSpellingLineNumber(
49 PrevTok->getLocation()) != SemicolonLine))
52 const SourceLocation LocEnd = Semicolon->getEndLoc();
53 const FileID FID = SM.getFileID(LocEnd);
54 const 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());
59 if (Lexer.LexFromRawLexer(Token))
62 const unsigned BaseIndent =
63 SM.getSpellingColumnNumber(Statement->getBeginLoc());
64 const unsigned NewTokenIndent =
65 SM.getSpellingColumnNumber(Token.getLocation());
66 const unsigned NewTokenLine = SM.getSpellingLineNumber(Token.getLocation());
68 if (!IsIfStmt && NewTokenIndent <= BaseIndent &&
69 Token.getKind() != tok::l_brace && NewTokenLine != SemicolonLine)
72 diag(LocStart,
"potentially unintended semicolon")
73 << FixItHint::CreateRemoval(SourceRange(LocStart, LocEnd));
std::optional< Token > getPreviousToken(SourceLocation Location, const SourceManager &SM, const LangOptions &LangOpts, bool SkipComments)
Returns previous token or std::nullopt if not found.