clang-tools  15.0.0git
InsertionPoint.h
Go to the documentation of this file.
1 //===--- InsertionPoint.h - Where should we add new code? --------*- 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 #include "clang/AST/DeclCXX.h"
10 #include "clang/Basic/Specifiers.h"
11 #include "clang/Tooling/Core/Replacement.h"
12 
13 namespace clang {
14 namespace clangd {
15 
16 // An anchor describes where to insert code into a decl sequence.
17 //
18 // It allows inserting above or below a block of decls matching some criterion.
19 // For example, "insert after existing constructors".
20 struct Anchor {
21  // A predicate describing which decls are considered part of a block.
22  // Match need not handle TemplateDecls, which are unwrapped before matching.
23  std::function<bool(const Decl *)> Match;
24  // Whether the insertion point should be before or after the matching block.
25  enum Dir { Above, Below } Direction = Below;
26 };
27 
28 // Returns the point to insert a declaration according to Anchors.
29 // Anchors are tried in order. For each, the first matching location is chosen.
30 SourceLocation insertionPoint(const DeclContext &Ctx,
31  llvm::ArrayRef<Anchor> Anchors);
32 
33 // Returns an edit inserting Code inside Ctx.
34 // Location is chosen according to Anchors, falling back to the end of Ctx.
35 // Fails if the chosen insertion point is in a different file than Ctx itself.
36 llvm::Expected<tooling::Replacement> insertDecl(llvm::StringRef Code,
37  const DeclContext &Ctx,
38  llvm::ArrayRef<Anchor> Anchors);
39 
40 // Variant for C++ classes that ensures the right access control.
41 SourceLocation insertionPoint(const CXXRecordDecl &InClass,
42  std::vector<Anchor> Anchors,
43  AccessSpecifier Protection);
44 
45 // Variant for C++ classes that ensures the right access control.
46 // May insert a new access specifier if needed.
47 llvm::Expected<tooling::Replacement> insertDecl(llvm::StringRef Code,
48  const CXXRecordDecl &InClass,
49  std::vector<Anchor> Anchors,
50  AccessSpecifier Protection);
51 
52 } // namespace clangd
53 } // namespace clang
clang::clangd::Anchor
Definition: InsertionPoint.h:20
clang::clangd::Anchor::Below
@ Below
Definition: InsertionPoint.h:25
clang::clangd::insertDecl
llvm::Expected< tooling::Replacement > insertDecl(llvm::StringRef Code, const DeclContext &DC, llvm::ArrayRef< Anchor > Anchors)
Definition: InsertionPoint.cpp:111
clang::clangd::Anchor::Dir
Dir
Definition: InsertionPoint.h:25
Ctx
Context Ctx
Definition: TUScheduler.cpp:547
clang::clangd::Anchor::Direction
enum clang::clangd::Anchor::Dir Direction
clang::clangd::insertionPoint
SourceLocation insertionPoint(const DeclContext &DC, llvm::ArrayRef< Anchor > Anchors)
Definition: InsertionPoint.cpp:96
Code
std::string Code
Definition: FindTargetTests.cpp:67
Decl
const FunctionDecl * Decl
Definition: AvoidBindCheck.cpp:100
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::Anchor::Above
@ Above
Definition: InsertionPoint.h:25
clang::clangd::Anchor::Match
std::function< bool(const Decl *)> Match
Definition: InsertionPoint.h:23