1 //===--- BracesAroundStatement.h - clang-tidy ------- -----------*- C++ -*-===//
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 #include "clang/AST/Stmt.h"
15 #include "clang/Basic/Diagnostic.h"
16 #include "clang/Basic/SourceLocation.h"
17 #include "clang/Basic/SourceManager.h"
18 
19 namespace clang::tidy::utils {
20 
21 /// A provider of fix-it hints to insert opening and closing braces. An instance
22 /// of this type is the result of calling `getBraceInsertionsHints` below.
23 struct BraceInsertionHints {
24   /// The position of a potential diagnostic. It coincides with the position of
25   /// the opening brace to insert, but can also just be the place to show a
26   /// diagnostic in case braces cannot be inserted automatically.
27   SourceLocation DiagnosticPos;
28 
29   /// Constructor for a no-hint.
30   BraceInsertionHints() = default;
31 
32   /// Constructor for a valid hint that cannot insert braces automatically.
BraceInsertionHintsBraceInsertionHints33   BraceInsertionHints(SourceLocation DiagnosticPos)
34       : DiagnosticPos(DiagnosticPos) {}
35 
36   /// Constructor for a hint offering fix-its for brace insertion. Both
37   /// positions must be valid.
BraceInsertionHintsBraceInsertionHints38   BraceInsertionHints(SourceLocation OpeningBracePos,
39                       SourceLocation ClosingBracePos, std::string ClosingBrace)
40       : DiagnosticPos(OpeningBracePos), OpeningBracePos(OpeningBracePos),
41         ClosingBracePos(ClosingBracePos), ClosingBrace(ClosingBrace) {
42     assert(offersFixIts());
43   }
44 
45   /// Indicates whether the hint provides at least the position of a diagnostic.
46   operator bool() const;
47 
48   /// Indicates whether the hint provides fix-its to insert braces.
49   bool offersFixIts() const;
50 
51   /// The number of lines between the inserted opening brace and its closing
52   /// counterpart.
53   unsigned resultingCompoundLineExtent(const SourceManager &SourceMgr) const;
54 
55   /// Fix-it to insert an opening brace.
56   FixItHint openingBraceFixIt() const;
57 
58   /// Fix-it to insert a closing brace.
59   FixItHint closingBraceFixIt() const;
60 
61 private:
62   SourceLocation OpeningBracePos;
63   SourceLocation ClosingBracePos;
64   std::string ClosingBrace;
65 };
66 
67 /// Create fix-it hints for braces that wrap the given statement when applied.
68 /// The algorithm computing them respects comment before and after the statement
69 /// and adds line breaks before the braces accordingly.
70 BraceInsertionHints
71 getBraceInsertionsHints(const Stmt *const S, const LangOptions &LangOpts,
72                         const SourceManager &SM, SourceLocation StartLoc,
73                         SourceLocation EndLocHint = SourceLocation());
74 
75 } // namespace clang::tidy::utils
76