clang-tools 22.0.0git
BracesAroundStatement.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file provides utilities to put braces around a statement.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_BRACESAROUNDSTATEMENT_H
15#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_BRACESAROUNDSTATEMENT_H
16
17#include "clang/AST/Stmt.h"
18#include "clang/Basic/Diagnostic.h"
19#include "clang/Basic/SourceLocation.h"
20#include "clang/Basic/SourceManager.h"
21
22namespace clang::tidy::utils {
23
24/// A provider of fix-it hints to insert opening and closing braces. An instance
25/// of this type is the result of calling `getBraceInsertionsHints` below.
27 /// The position of a potential diagnostic. It coincides with the position of
28 /// the opening brace to insert, but can also just be the place to show a
29 /// diagnostic in case braces cannot be inserted automatically.
30 SourceLocation DiagnosticPos;
31
32 /// Constructor for a no-hint.
34
35 /// Constructor for a valid hint that cannot insert braces automatically.
38
39 /// Constructor for a hint offering fix-its for brace insertion. Both
40 /// positions must be valid.
41 BraceInsertionHints(SourceLocation OpeningBracePos,
42 SourceLocation ClosingBracePos, StringRef ClosingBrace)
43 : DiagnosticPos(OpeningBracePos), OpeningBracePos(OpeningBracePos),
44 ClosingBracePos(ClosingBracePos), ClosingBrace(ClosingBrace) {
45 assert(offersFixIts());
46 }
47
48 /// Indicates whether the hint provides at least the position of a diagnostic.
49 operator bool() const;
50
51 /// Indicates whether the hint provides fix-its to insert braces.
52 bool offersFixIts() const;
53
54 /// The number of lines between the inserted opening brace and its closing
55 /// counterpart.
56 unsigned resultingCompoundLineExtent(const SourceManager &SourceMgr) const;
57
58 /// Fix-it to insert an opening brace.
59 FixItHint openingBraceFixIt() const;
60
61 /// Fix-it to insert a closing brace.
62 FixItHint closingBraceFixIt() const;
63
64private:
65 SourceLocation OpeningBracePos;
66 SourceLocation ClosingBracePos;
67 StringRef ClosingBrace;
68};
69
70/// Create fix-it hints for braces that wrap the given statement when applied.
71/// The algorithm computing them respects comment before and after the statement
72/// and adds line breaks before the braces accordingly.
73BraceInsertionHints
74getBraceInsertionsHints(const Stmt *S, const LangOptions &LangOpts,
75 const SourceManager &SM, SourceLocation StartLoc,
76 SourceLocation EndLocHint = SourceLocation());
77
78} // namespace clang::tidy::utils
79
80#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_BRACESAROUNDSTATEMENT_H
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.
BraceInsertionHints()=default
Constructor for a no-hint.
SourceLocation DiagnosticPos
The position of a potential diagnostic.
FixItHint closingBraceFixIt() const
Fix-it to insert a closing brace.
BraceInsertionHints(SourceLocation DiagnosticPos)
Constructor for a valid hint that cannot insert braces automatically.
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.
BraceInsertionHints(SourceLocation OpeningBracePos, SourceLocation ClosingBracePos, StringRef ClosingBrace)
Constructor for a hint offering fix-its for brace insertion.