81 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8///9/// \file10/// This file provides utilities to put braces around a statement.11///12//===----------------------------------------------------------------------===//13 14#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_BRACESAROUNDSTATEMENT_H15#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_BRACESAROUNDSTATEMENT_H16 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 instance25/// of this type is the result of calling `getBraceInsertionsHints` below.26struct BraceInsertionHints {27 /// The position of a potential diagnostic. It coincides with the position of28 /// the opening brace to insert, but can also just be the place to show a29 /// diagnostic in case braces cannot be inserted automatically.30 SourceLocation DiagnosticPos;31 32 /// Constructor for a no-hint.33 BraceInsertionHints() = default;34 35 /// Constructor for a valid hint that cannot insert braces automatically.36 BraceInsertionHints(SourceLocation DiagnosticPos)37 : DiagnosticPos(DiagnosticPos) {}38 39 /// Constructor for a hint offering fix-its for brace insertion. Both40 /// 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 closing55 /// 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 statement72/// and adds line breaks before the braces accordingly.73BraceInsertionHints74getBraceInsertionsHints(const Stmt *S, const LangOptions &LangOpts,75 const SourceManager &SM, SourceLocation StartLoc,76 SourceLocation EndLocHint = SourceLocation());77 78} // namespace clang::tidy::utils79 80#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_BRACESAROUNDSTATEMENT_H81