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"
33std::string
MakeLineColumnName(
const SourceManager& sm,
const SourceLocation& loc,
const std::string_view& prefix);
38 return SC_Static == md->getStorageClass();
44 return decl and decl->getType()->isReferenceType();
57inline bool Contains(
const std::string_view source,
const std::string_view search)
59 return std::string::npos != source.find(search, 0);
63template<
typename K,
typename V,
typename U>
64inline bool Contains(
const llvm::DenseMap<K, V>& map,
const U& key)
66 return map.find(key) != map.end();
70void ReplaceAll(std::string& str, std::string_view from, std::string_view to);
73void InsertBefore(std::string& source,
const std::string_view& find,
const std::string_view& replace);
76inline const SourceManager&
GetSM(
const Decl& decl)
78 return decl.getASTContext().getSourceManager();
84 return decl.getASTContext().getLangOpts();
108std::string
GetName(
const DeclRefExpr& declRefExpr);
109std::string
GetName(
const VarDecl& VD);
110std::string
GetName(
const TemplateParamObjectDecl& decl);
114std::string
GetName(
const NamedDecl& nd,
const QualifiedName qualifiedName = QualifiedName::No);
128std::string
GetName(
const CXXRecordDecl& RD);
131std::string
GetName(
const CXXTemporaryObjectExpr& tmp);
148 return cxxRecordDecl->getName().empty();
156 const TemplateTypeParmDecl* decl,
157 const bool isParameter,
158 const TemplateTypeParmType* type =
nullptr);
173std::string
GetName(
const QualType& t,
const Unqualified unqualified = Unqualified::No);
180GetTypeNameAsParameter(
const QualType& t, std::string_view varName,
const Unqualified unqualified = Unqualified::No);
186std::string
GetNestedName(
const NestedNameSpecifier* nns,
const IgnoreNamespace ignoreNamespace = IgnoreNamespace::No);
188 WithTemplateParameters withTemplateParameters = WithTemplateParameters::No);
191const std::string
GetNoExcept(
const FunctionDecl& decl);
192const std::string_view
GetConst(
const FunctionDecl& decl);
198uint64_t
GetSize(
const ConstantArrayType*);
201template<
typename QT,
typename SUB_T>
204 if(
const auto* lref = dyn_cast_or_null<QT>(t.getTypePtrOrNull())) {
206 const auto& ct = subType.getCanonicalType();
207 const auto* plainSubType = ct.getTypePtrOrNull();
209 return isa<SUB_T>(plainSubType);
216template<
typename T,
typename TFunc>
219 for(; start < end; ++start) {
282 static inline std::string mScope;
288#define SCOPE_HELPER(d) \
289 ScopeHandler _scopeHandler \
303 : ::llvm::raw_string_ostream{mData}
307 void Print(
const TemplateArgument&);
308 void Print(
const TemplateSpecializationType&);
309 void Print(
const TemplateParamObjectDecl&);
310 void Print(
const TypeConstraint&);
311 void Print(
const StringLiteral&);
312 void Print(
const CharacterLiteral&);
322 : mAction{std::forward<T>(action)}
345 constexpr bool any_of(
const auto&... ts)
const {
return ((
t == ts) or ...); }
363 requires(std::derived_from<T, CodeGenerator> or std::same_as<T, OutputFormatHelper>)
367 const LambdaExpr* mLambdaExpr{};
371 : mConsumer{consumer}
384 const LambdaExpr*
Get()
const {
return mLambdaExpr; }
388template<
typename T,
typename U>
398 mValue = std::forward<U>(newVal);
410 using std::optional<T>::optional;
412 template<
class Return>
414 requires(not std::is_pointer_v<T>)
416 if(not this->has_value()) {
420 return func(this->value());
423 template<
class Return>
425 requires(std::is_pointer_v<T>)
427 if(not this->has_value()) {
431 if(this->value() ==
nullptr) {
434 return func(*this->value());
438 template<
class Return>
441 if(not this->has_value()) {
445 if(not func(this->value()).has_value()) {
#define STRONG_BOOL(typeName)
A more than simple typsafe bool.
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_then(std::function< MyOptional< Return >(T)> func)
MyOptional< Return > and_then(std::function< MyOptional< Return >(std::remove_pointer_t< T > &)> func)
MyOptional< Return > and_not(std::function< MyOptional< Return >(T)> func)
! Find a LambdaExpr inside a Decltype
constexpr P0315Visitor(T &consumer)
const LambdaExpr * Get() const
bool VisitLambdaExpr(const LambdaExpr *expr)
The ScopeHandler tracks the current scope.
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)
const SourceManager & GetSM(const Decl &decl)
bool Contains(const std::string_view source, const std::string_view search)
const LangOptions & GetLangOpts(const Decl &decl)
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 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)
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)
void P0315Visitor_HandleLambdaExpr(OutputFormatHelper &ofm, const LambdaExpr *expr)
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