aboutsummaryrefslogtreecommitdiff
path: root/clang-tools-extra/clang-tidy/utils/BracesAroundStatement.h
blob: cb1c06c7aa1a1a9d5bdd56cd805aa26f19e56c8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//===--- BracesAroundStatement.h - clang-tidy ------- -----------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file provides utilities to put braces around a statement.
///
//===----------------------------------------------------------------------===//

#include "clang/AST/Stmt.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/SourceManager.h"

namespace clang::tidy::utils {

/// A provider of fix-it hints to insert opening and closing braces. An instance
/// of this type is the result of calling `getBraceInsertionsHints` below.
struct BraceInsertionHints {
  /// The position of a potential diagnostic. It coincides with the position of
  /// the opening brace to insert, but can also just be the place to show a
  /// diagnostic in case braces cannot be inserted automatically.
  SourceLocation DiagnosticPos;

  /// Constructor for a no-hint.
  BraceInsertionHints() = default;

  /// Constructor for a valid hint that cannot insert braces automatically.
  BraceInsertionHints(SourceLocation DiagnosticPos)
      : DiagnosticPos(DiagnosticPos) {}

  /// Constructor for a hint offering fix-its for brace insertion. Both
  /// positions must be valid.
  BraceInsertionHints(SourceLocation OpeningBracePos,
                      SourceLocation ClosingBracePos, std::string ClosingBrace)
      : DiagnosticPos(OpeningBracePos), OpeningBracePos(OpeningBracePos),
        ClosingBracePos(ClosingBracePos), ClosingBrace(ClosingBrace) {
    assert(offersFixIts());
  }

  /// Indicates whether the hint provides at least the position of a diagnostic.
  operator bool() const;

  /// Indicates whether the hint provides fix-its to insert braces.
  bool offersFixIts() const;

  /// The number of lines between the inserted opening brace and its closing
  /// counterpart.
  unsigned resultingCompoundLineExtent(const SourceManager &SourceMgr) const;

  /// Fix-it to insert an opening brace.
  FixItHint openingBraceFixIt() const;

  /// Fix-it to insert a closing brace.
  FixItHint closingBraceFixIt() const;

private:
  SourceLocation OpeningBracePos;
  SourceLocation ClosingBracePos;
  std::string ClosingBrace;
};

/// Create fix-it hints for braces that wrap the given statement when applied.
/// The algorithm computing them respects comment before and after the statement
/// and adds line breaks before the braces accordingly.
BraceInsertionHints
getBraceInsertionsHints(const Stmt *const S, const LangOptions &LangOpts,
                        const SourceManager &SM, SourceLocation StartLoc,
                        SourceLocation EndLocHint = SourceLocation());

} // namespace clang::tidy::utils