Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:37:12

0001 //===--- SourceCodeBuilders.h - Source-code building facilities -*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 ///
0009 /// \file
0010 /// This file collects facilities for generating source code strings.
0011 ///
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_CLANG_TOOLING_TRANSFORMER_SOURCECODEBUILDERS_H
0015 #define LLVM_CLANG_TOOLING_TRANSFORMER_SOURCECODEBUILDERS_H
0016 
0017 #include "clang/AST/ASTContext.h"
0018 #include "clang/AST/Expr.h"
0019 #include <string>
0020 
0021 namespace clang {
0022 namespace tooling {
0023 
0024 /// \name Code analysis utilities.
0025 /// @{
0026 /// Ignores implicit object-construction expressions in addition to the normal
0027 /// implicit expressions that are ignored.
0028 const Expr *reallyIgnoreImplicit(const Expr &E);
0029 
0030 /// Determines whether printing this expression in *any* expression requires
0031 /// parentheses to preserve its meaning. This analyses is necessarily
0032 /// conservative because it lacks information about the target context.
0033 bool mayEverNeedParens(const Expr &E);
0034 
0035 /// Determines whether printing this expression to the left of a dot or arrow
0036 /// operator requires a parentheses to preserve its meaning. Given that
0037 /// dot/arrow are (effectively) the highest precedence, this is equivalent to
0038 /// asking whether it ever needs parens.
0039 inline bool needParensBeforeDotOrArrow(const Expr &E) {
0040   return mayEverNeedParens(E);
0041 }
0042 
0043 /// Determines whether printing this expression to the right of a unary operator
0044 /// requires a parentheses to preserve its meaning.
0045 bool needParensAfterUnaryOperator(const Expr &E);
0046 
0047 // Recognizes known types (and sugared versions thereof) that overload the `*`
0048 // and `->` operator. Below is the list of currently included types, but it is
0049 // subject to change:
0050 //
0051 // * std::unique_ptr, std::shared_ptr, std::weak_ptr,
0052 // * std::optional, absl::optional, llvm::Optional,
0053 // * absl::StatusOr, llvm::Expected.
0054 bool isKnownPointerLikeType(QualType Ty, ASTContext &Context);
0055 /// @}
0056 
0057 /// \name Basic code-string generation utilities.
0058 /// @{
0059 
0060 /// Builds source for an expression, adding parens if needed for unambiguous
0061 /// parsing.
0062 std::optional<std::string> buildParens(const Expr &E,
0063                                        const ASTContext &Context);
0064 
0065 /// Builds idiomatic source for the dereferencing of `E`: prefix with `*` but
0066 /// simplify when it already begins with `&`.  \returns empty string on failure.
0067 std::optional<std::string> buildDereference(const Expr &E,
0068                                             const ASTContext &Context);
0069 
0070 /// Builds idiomatic source for taking the address of `E`: prefix with `&` but
0071 /// simplify when it already begins with `*`.  \returns empty string on failure.
0072 std::optional<std::string> buildAddressOf(const Expr &E,
0073                                           const ASTContext &Context);
0074 
0075 /// Adds a dot to the end of the given expression, but adds parentheses when
0076 /// needed by the syntax, and simplifies to `->` when possible, e.g.:
0077 ///
0078 ///  `x` becomes `x.`
0079 ///  `*a` becomes `a->`
0080 ///  `a+b` becomes `(a+b).`
0081 ///
0082 /// DEPRECATED. Use `buildAccess`.
0083 std::optional<std::string> buildDot(const Expr &E, const ASTContext &Context);
0084 
0085 /// Adds an arrow to the end of the given expression, but adds parentheses
0086 /// when needed by the syntax, and simplifies to `.` when possible, e.g.:
0087 ///
0088 ///  `x` becomes `x->`
0089 ///  `&a` becomes `a.`
0090 ///  `a+b` becomes `(a+b)->`
0091 ///
0092 /// DEPRECATED. Use `buildAccess`.
0093 std::optional<std::string> buildArrow(const Expr &E, const ASTContext &Context);
0094 
0095 /// Specifies how to classify pointer-like types -- like values or like pointers
0096 /// -- with regard to generating member-access syntax.
0097 enum class PLTClass : bool {
0098   Value,
0099   Pointer,
0100 };
0101 
0102 /// Adds an appropriate access operator (`.`, `->` or nothing, in the case of
0103 /// implicit `this`) to the end of the given expression. Adds parentheses when
0104 /// needed by the syntax and simplifies when possible. If `PLTypeClass` is
0105 /// `Pointer`, for known pointer-like types (see `isKnownPointerLikeType`),
0106 /// treats `operator->` and `operator*` like the built-in `->` and `*`
0107 /// operators.
0108 ///
0109 ///  `x` becomes `x->` or `x.`, depending on `E`'s type
0110 ///  `a+b` becomes `(a+b)->` or `(a+b).`, depending on `E`'s type
0111 ///  `&a` becomes `a.`
0112 ///  `*a` becomes `a->`
0113 std::optional<std::string>
0114 buildAccess(const Expr &E, ASTContext &Context,
0115             PLTClass Classification = PLTClass::Pointer);
0116 /// @}
0117 
0118 } // namespace tooling
0119 } // namespace clang
0120 #endif // LLVM_CLANG_TOOLING_TRANSFORMER_SOURCECODEBUILDERS_H