15#include "../utils/LexerUtils.h"
17#include "clang/AST/ASTContext.h"
18#include "clang/Basic/CharInfo.h"
19#include "clang/Basic/LangOptions.h"
20#include "clang/Lex/Lexer.h"
24BraceInsertionHints::operator bool()
const {
return DiagnosticPos.isValid(); }
27 return OpeningBracePos.isValid() && ClosingBracePos.isValid();
31 const SourceManager &SourceMgr)
const {
32 return SourceMgr.getSpellingLineNumber(ClosingBracePos) -
33 SourceMgr.getSpellingLineNumber(OpeningBracePos);
37 return OpeningBracePos.isValid()
38 ? FixItHint::CreateInsertion(OpeningBracePos,
" {")
43 return ClosingBracePos.isValid()
44 ? FixItHint::CreateInsertion(ClosingBracePos, ClosingBrace)
49 const LangOptions &LangOpts) {
51 SourceLocation Beginning = Lexer::GetBeginningOfToken(
Loc, SM, LangOpts);
52 const bool Invalid = Lexer::getRawToken(Beginning, Tok, SM, LangOpts);
53 assert(!Invalid &&
"Expected a valid token.");
56 return tok::NUM_TOKENS;
62 const LangOptions &LangOpts) {
68 Loc =
Loc.getLocWithOffset(1);
71 assert(
Loc.isValid());
72 while (isHorizontalWhitespace(*SM.getCharacterData(
Loc))) {
73 Loc =
Loc.getLocWithOffset(1);
76 if (isVerticalWhitespace(*SM.getCharacterData(
Loc))) {
81 if (TokKind != tok::comment) {
86 SourceLocation TokEndLoc = Lexer::getLocForEndOfToken(
Loc, 0, SM, LangOpts);
87 SourceRange TokRange(
Loc, TokEndLoc);
88 StringRef Comment = Lexer::getSourceText(
89 CharSourceRange::getTokenRange(TokRange), SM, LangOpts);
90 if (Comment.starts_with(
"/*") && Comment.contains(
'\n')) {
103 const LangOptions &LangOpts,
104 const SourceManager &SM,
105 SourceLocation StartLoc,
106 SourceLocation EndLocHint) {
114 if (!S || isa<CompoundStmt>(S)) {
121 const SourceLocation StmtBeginLoc = S->getBeginLoc();
122 if (isa<NullStmt>(S) && StmtBeginLoc.isValid() &&
123 getTokenKind(StmtBeginLoc, SM, LangOpts) == tok::l_brace)
126 if (StartLoc.isInvalid())
132 StartLoc = Lexer::makeFileCharRange(
133 CharSourceRange::getCharRange(StartLoc, S->getBeginLoc()), SM,
136 if (StartLoc.isInvalid())
138 StartLoc = Lexer::getLocForEndOfToken(StartLoc, 0, SM, LangOpts);
141 SourceLocation EndLoc;
142 std::string ClosingInsertion;
143 if (EndLocHint.isValid()) {
145 ClosingInsertion =
"} ";
148 ClosingInsertion =
"\n}";
151 assert(StartLoc.isValid());
159 if (Lexer::makeFileCharRange(
160 CharSourceRange::getTokenRange(SourceRange(
161 SM.getSpellingLoc(StartLoc), SM.getSpellingLoc(EndLoc))),
165 return {StartLoc, EndLoc, ClosingInsertion};
This file provides utilities to put braces around a statement.
SourceLocation getUnifiedEndLoc(const Stmt &S, const SourceManager &SM, const LangOptions &LangOpts)
Stmt->getEndLoc does not always behave the same way depending on Token type.
static SourceLocation findEndLocation(const Stmt &S, const SourceManager &SM, const LangOptions &LangOpts)
BraceInsertionHints getBraceInsertionsHints(const Stmt *const S, const LangOptions &LangOpts, const SourceManager &SM, SourceLocation StartLoc, SourceLocation EndLocHint)
Create fix-it hints for braces that wrap the given statement when applied.
static tok::TokenKind getTokenKind(SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts)
A provider of fix-it hints to insert opening and closing braces.
FixItHint closingBraceFixIt() const
Fix-it to insert a closing brace.
bool offersFixIts() const
Indicates whether the hint provides fix-its to insert braces.
unsigned resultingCompoundLineExtent(const SourceManager &SourceMgr) const
The number of lines between the inserted opening brace and its closing counterpart.
FixItHint openingBraceFixIt() const
Fix-it to insert an opening brace.