8 #ifndef INSIGHTS_HELPERS_H
9 #define INSIGHTS_HELPERS_H
12 #include "clang/AST/AST.h"
13 #include "clang/AST/ASTContext.h"
14 #include "clang/AST/RecursiveASTVisitor.h"
15 #include "clang/Lex/Lexer.h"
34 std::string
MakeLineColumnName(
const SourceManager& sm,
const SourceLocation& loc,
const std::string_view& prefix);
39 return SC_Static == md->getStorageClass();
45 return decl and decl->getType()->isReferenceType();
58 inline bool Contains(
const std::string_view source,
const std::string_view search)
60 return std::string::npos != source.find(search, 0);
64 template<
typename K,
typename V,
typename U>
65 inline bool Contains(
const llvm::DenseMap<K, V>& map,
const U& key)
67 return map.find(key) != map.end();
71 void ReplaceAll(std::string& str, std::string_view from, std::string_view to);
74 void InsertBefore(std::string& source,
const std::string_view& find,
const std::string_view& replace);
77 inline const SourceManager&
GetSM(
const Decl& decl)
79 return decl.getASTContext().getSourceManager();
85 return decl.getASTContext().getLangOpts();
109 std::string
GetName(
const DeclRefExpr& declRefExpr);
110 std::string
GetName(
const VarDecl& VD);
111 std::string
GetName(
const TemplateParamObjectDecl& decl);
115 std::string
GetName(
const NamedDecl& nd,
const QualifiedName qualifiedName = QualifiedName::No);
129 std::string
GetName(
const CXXRecordDecl& RD);
132 std::string
GetName(
const CXXTemporaryObjectExpr& tmp);
149 return cxxRecordDecl->getName().empty();
157 const TemplateTypeParmDecl* decl,
158 const bool isParameter,
159 const TemplateTypeParmType* type =
nullptr);
174 std::string
GetName(
const QualType& t,
const Unqualified unqualified = Unqualified::No);
181 GetTypeNameAsParameter(
const QualType& t, std::string_view varName,
const Unqualified unqualified = Unqualified::No);
187 std::string
GetNestedName(
const NestedNameSpecifier* nns,
const IgnoreNamespace ignoreNamespace = IgnoreNamespace::No);
189 WithTemplateParameters withTemplateParameters = WithTemplateParameters::No);
192 const std::string
GetNoExcept(
const FunctionDecl& decl);
193 const std::string_view
GetConst(
const FunctionDecl& decl);
199 uint64_t
GetSize(
const ConstantArrayType*);
202 template<
typename QT,
typename SUB_T>
205 if(
const auto* lref = dyn_cast_or_null<QT>(t.getTypePtrOrNull())) {
207 const auto& ct = subType.getCanonicalType();
208 const auto* plainSubType = ct.getTypePtrOrNull();
210 return isa<SUB_T>(plainSubType);
217 template<
typename T,
typename TFunc>
220 for(; start < end; ++start) {
283 static inline std::string mScope;
289 #define SCOPE_HELPER(d) \
290 ScopeHandler _scopeHandler \
304 : ::llvm::raw_string_ostream{mData}
308 void Print(
const TemplateArgument&);
309 void Print(
const TemplateSpecializationType&);
310 void Print(
const TemplateParamObjectDecl&);
311 void Print(
const TypeConstraint&);
312 void Print(
const StringLiteral&);
313 void Print(
const CharacterLiteral&);
323 : mAction{std::forward<T>(action)}
346 constexpr
bool any_of(
const auto&... ts)
const {
return ((
t == ts) or ...); }
361 std::variant<std::reference_wrapper<class OutputFormatHelper>, std::reference_wrapper<class CodeGenerator>>
363 const LambdaExpr* mLambdaExpr{};
378 const LambdaExpr*
Get()
const {
return mLambdaExpr; }
382 template<
typename T,
typename U>
392 mValue = std::forward<U>(newVal);
404 using std::optional<T>::optional;
406 template<
class Return>
408 requires(not std::is_pointer_v<T>)
410 if(not this->has_value()) {
414 return func(this->value());
417 template<
class Return>
419 requires(std::is_pointer_v<T>)
421 if(not this->has_value()) {
425 if(this->value() ==
nullptr) {
428 return func(*this->value());
432 template<
class Return>
435 if(not this->has_value()) {
439 if(not func(this->value()).has_value()) {
StackList is a container for a list which elements exist only on the stack.
More or less the heart of C++ Insights.
A helper which invokes a lambda when the scope is destroyed.
MyOptional< Return > and_not(std::function< MyOptional< Return >(T)> func)
MyOptional< Return > and_then(std::function< MyOptional< Return >(T)> func) requires(not std
MyOptional< Return > and_then(std::function< MyOptional< Return >(std::remove_pointer_t< T > &)> func) requires(std
! Find a LambdaExpr inside a Decltype
P0315Visitor(class OutputFormatHelper &ofm)
const LambdaExpr * Get() const
P0315Visitor(class CodeGenerator &cg)
bool VisitLambdaExpr(const LambdaExpr *expr)
The ScopeHandler tracks the current scope.
ScopeHandler(const Decl *d)
static std::string RemoveCurrentScope(std::string name)
Remove the current scope from a string.
Specialization for ::llvm::raw_string_ostream with an internal std::string buffer.
void Print(const TemplateArgument &)
void InsertBefore(std::string &source, const std::string_view &find, const std::string_view &replace)
uint64_t GetSize(const ConstantArrayType *arrayType)
std::string GetLambdaName(const CXXRecordDecl &lambda)
bool Contains(const std::string_view source, const std::string_view search)
std::string BuildRetTypeName(const Decl &decl)
bool IsEvaluatable(const VarDecl &varDecl)
Check whether a VarDecls initialization can be done a compile-time.
const std::string GetNoExcept(const FunctionDecl &decl)
const SourceManager & GetSM(const Decl &decl)
const DeclRefExpr * FindDeclRef(const Stmt *stmt)
Go deep in a Stmt if necessary and look to all childs for a DeclRefExpr.
std::string GetPlainName(const DeclRefExpr &DRE)
std::string GetElaboratedTypeKeyword(const ElaboratedTypeKeyword keyword)
std::string MakeLineColumnName(const SourceManager &sm, const SourceLocation &loc, const std::string_view &prefix)
QualType GetType(QualType t)
In Cfront mode we transform references to pointers.
std::string GetName(const NamedDecl &nd, const QualifiedName qualifiedName)
STRONG_BOOL(InsightsSuppressScope)
const LangOptions & GetLangOpts(const Decl &decl)
void ReplaceAll(std::string &str, std::string_view from, std::string_view to)
std::string BuildInternalVarName(const std::string_view &varName)
const std::string_view GetConst(const FunctionDecl &decl)
std::string GetNameAsFunctionPointer(const QualType &t)
std::string GetTemporaryName(const Expr &tmp)
std::string BuildTemplateParamObjectName(std::string name)
bool IsReferenceType(const ValueDecl *decl)
void AppendTemplateTypeParamName(OutputFormatHelper &ofm, const TemplateTypeParmDecl *decl, const bool isParameter, const TemplateTypeParmType *type)
std::string GetDeclContext(const DeclContext *ctx, WithTemplateParameters withTemplateParameters)
const QualType GetDesugarType(const QualType &QT)
Remove decltype from a QualType, if possible.
static bool TypeContainsSubType(const QualType &t)
static std::string GetUnqualifiedScopelessName(const Type *type, const InsightsSuppressScope supressScope)
bool IsStaticStorageClass(const CXXMethodDecl *md)
APValue * GetEvaluatedValue(const VarDecl &varDecl)
Get the evaluated APValue from a VarDecl
std::string GetNestedName(const NestedNameSpecifier *nns, const IgnoreNamespace ignoreNamespace)
void for_each(T start, T end, TFunc &&func)
std::string GetTypeNameAsParameter(const QualType &t, std::string_view varName, const Unqualified unqualified)
bool IsAnonymousStructOrUnion(const CXXRecordDecl *cxxRecordDecl)
Check whether this is an anonymous struct or union.
static bool IsTrivialStaticClassVarDecl(const DeclRefExpr &declRefExpr)
QualType GetDesugarReturnType(const FunctionDecl &FD)
Base class for StackList.
BackupAndRestore(T &value, U &&newVal)
Track the scope we are currently in to build a properly scoped variable.
ScopeHelper(const size_t len)
const size_t mLength
Length of the scope as it was before this declaration was appended.
Handy helper to avoid longish comparisons.
constexpr bool any_of(const auto &... ts) const