• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--- SourceCodeBuilders.h - Source-code building facilities -*- 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 collects facilities for generating source code strings.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_TOOLING_TRANSFORMER_SOURCE_CODE_BUILDERS_H_
15 #define LLVM_CLANG_TOOLING_TRANSFORMER_SOURCE_CODE_BUILDERS_H_
16 
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/Expr.h"
19 #include <string>
20 
21 namespace clang {
22 namespace tooling {
23 
24 /// \name Code analysis utilities.
25 /// @{
26 /// Ignores implicit object-construction expressions in addition to the normal
27 /// implicit expressions that are ignored.
28 const Expr *reallyIgnoreImplicit(const Expr &E);
29 
30 /// Determines whether printing this expression in *any* expression requires
31 /// parentheses to preserve its meaning. This analyses is necessarily
32 /// conservative because it lacks information about the target context.
33 bool mayEverNeedParens(const Expr &E);
34 
35 /// Determines whether printing this expression to the left of a dot or arrow
36 /// operator requires a parentheses to preserve its meaning. Given that
37 /// dot/arrow are (effectively) the highest precedence, this is equivalent to
38 /// asking whether it ever needs parens.
needParensBeforeDotOrArrow(const Expr & E)39 inline bool needParensBeforeDotOrArrow(const Expr &E) {
40   return mayEverNeedParens(E);
41 }
42 
43 /// Determines whether printing this expression to the right of a unary operator
44 /// requires a parentheses to preserve its meaning.
45 bool needParensAfterUnaryOperator(const Expr &E);
46 /// @}
47 
48 /// \name Basic code-string generation utilities.
49 /// @{
50 
51 /// Builds source for an expression, adding parens if needed for unambiguous
52 /// parsing.
53 llvm::Optional<std::string> buildParens(const Expr &E,
54                                         const ASTContext &Context);
55 
56 /// Builds idiomatic source for the dereferencing of `E`: prefix with `*` but
57 /// simplify when it already begins with `&`.  \returns empty string on failure.
58 llvm::Optional<std::string> buildDereference(const Expr &E,
59                                              const ASTContext &Context);
60 
61 /// Builds idiomatic source for taking the address of `E`: prefix with `&` but
62 /// simplify when it already begins with `*`.  \returns empty string on failure.
63 llvm::Optional<std::string> buildAddressOf(const Expr &E,
64                                            const ASTContext &Context);
65 
66 /// Adds a dot to the end of the given expression, but adds parentheses when
67 /// needed by the syntax, and simplifies to `->` when possible, e.g.:
68 ///
69 ///  `x` becomes `x.`
70 ///  `*a` becomes `a->`
71 ///  `a+b` becomes `(a+b).`
72 llvm::Optional<std::string> buildDot(const Expr &E, const ASTContext &Context);
73 
74 /// Adds an arrow to the end of the given expression, but adds parentheses
75 /// when needed by the syntax, and simplifies to `.` when possible, e.g.:
76 ///
77 ///  `x` becomes `x->`
78 ///  `&a` becomes `a.`
79 ///  `a+b` becomes `(a+b)->`
80 llvm::Optional<std::string> buildArrow(const Expr &E,
81                                        const ASTContext &Context);
82 /// @}
83 
84 } // namespace tooling
85 } // namespace clang
86 #endif // LLVM_CLANG_TOOLING_TRANSFORMER_SOURCE_CODE_BUILDERS_H_
87