diff options
Diffstat (limited to 'src/StringUtils.h')
-rw-r--r-- | src/StringUtils.h | 90 |
1 files changed, 54 insertions, 36 deletions
diff --git a/src/StringUtils.h b/src/StringUtils.h index 8aaee009..b8744f9e 100644 --- a/src/StringUtils.h +++ b/src/StringUtils.h @@ -27,7 +27,6 @@ #include "clazy_export.h" #include "Utils.h" -#include "HierarchyUtils.h" #include "clazy_stl.h" #include "clang/AST/PrettyPrinter.h" @@ -42,7 +41,7 @@ class LangOpts; } -namespace StringUtils { +namespace clazy { // Returns the class name. // The name will not include any templates, so "QVector::iterator" would be returned for QVector<int>::iterator @@ -108,20 +107,60 @@ inline std::string classNameFor(clang::ParmVarDecl *param) return classNameFor(param->getType()); } +inline llvm::StringRef name(const clang::NamedDecl *decl) +{ + if (decl->getDeclName().isIdentifier()) + return decl->getName(); + + return ""; +} + +inline llvm::StringRef name(const clang::CXXMethodDecl *method) +{ + auto op = method->getOverloadedOperator(); + if (op == clang::OO_Subscript) + return "operator[]"; + if (op == clang::OO_LessLess) + return "operator<<"; + if (op == clang::OO_PlusEqual) + return "operator+="; + + return name(static_cast<const clang::NamedDecl *>(method)); +} + +inline llvm::StringRef name(const clang::CXXConstructorDecl *decl) +{ + return name(decl->getParent()); +} + +inline llvm::StringRef name(const clang::CXXDestructorDecl *decl) +{ + return name(decl->getParent()); +} + +// Returns the type name with or without namespace, depending on how it was written by the user. +// If the user omitted the namespace then the return won't have namespace +inline std::string name(clang::QualType t, clang::LangOptions lo, bool asWritten) +{ + clang::PrintingPolicy p(lo); + p.SuppressScope = asWritten; + return t.getAsString(p); +} + template <typename T> -inline bool isOfClass(T *node, const std::string &className) +inline bool isOfClass(T *node, llvm::StringRef className) { return node && classNameFor(node) == className; } -inline bool functionIsOneOf(clang::FunctionDecl *func, const std::vector<std::string> &functionNames) +inline bool functionIsOneOf(clang::FunctionDecl *func, const std::vector<llvm::StringRef> &functionNames) { - return func && clazy_std::contains(functionNames, func->getNameAsString()); + return func && clazy::contains(functionNames, clazy::name(func)); } -inline bool classIsOneOf(clang::CXXRecordDecl *record, const std::vector<std::string> &classNames) +inline bool classIsOneOf(clang::CXXRecordDecl *record, const std::vector<llvm::StringRef> &classNames) { - return record && clazy_std::contains(classNames, record->getNameAsString()); + return record && clazy::contains(classNames, clazy::name(record)); } inline void printLocation(const clang::SourceManager &sm, clang::SourceLocation loc, bool newLine = true) @@ -141,7 +180,7 @@ inline void printRange(const clang::SourceManager &sm, clang::SourceRange range, inline void printLocation(const clang::SourceManager &sm, const clang::Stmt *s, bool newLine = true) { if (s) - printLocation(sm, s->getLocStart(), newLine); + printLocation(sm, getLocStart(s), newLine); } inline void printLocation(const clang::PresumedLoc &loc, bool newLine = true) @@ -156,7 +195,7 @@ inline std::string qualifiedMethodName(clang::FunctionDecl *func) if (!func) return {}; - clang::CXXMethodDecl *method = clang::dyn_cast<clang::CXXMethodDecl>(func); + auto method = clang::dyn_cast<clang::CXXMethodDecl>(func); if (!method) return func->getQualifiedNameAsString(); @@ -172,27 +211,6 @@ inline std::string qualifiedMethodName(clang::CallExpr *call) return call ? qualifiedMethodName(call->getDirectCallee()) : std::string(); } -inline std::string methodName(clang::CallExpr *call) -{ - if (!call) - return {}; - - clang::FunctionDecl *func = call->getDirectCallee(); - return func ? func->getNameAsString() : std::string(); -} - -inline void printParents(clang::ParentMap *map, clang::Stmt *s) -{ - int level = 0; - llvm::errs() << (s ? s->getStmtClassName() : nullptr) << "\n"; - - while (clang::Stmt *parent = HierarchyUtils::parent(map, s)) { - ++level; - llvm::errs() << std::string(level, ' ') << parent->getStmtClassName() << "\n"; - s = parent; - } -} - inline std::string accessString(clang::AccessSpecifier s) { switch (s) @@ -247,14 +265,14 @@ inline std::string returnTypeName(clang::CallExpr *call, const clang::LangOption return {}; clang::FunctionDecl *func = call->getDirectCallee(); - return func ? StringUtils::typeName(func->getReturnType(), lo, simpleName) : std::string(); + return func ? clazy::typeName(func->getReturnType(), lo, simpleName) : std::string(); } -inline bool hasArgumentOfType(clang::FunctionDecl *func, const std::string &typeName, +inline bool hasArgumentOfType(clang::FunctionDecl *func, llvm::StringRef typeName, const clang::LangOptions &lo, bool simpleName = true) { - return clazy_std::any_of(Utils::functionParameters(func), [simpleName,lo,typeName](clang::ParmVarDecl *param) { - return StringUtils::typeName(param->getType(), lo, simpleName) == typeName; + return clazy::any_of(Utils::functionParameters(func), [simpleName,lo,typeName](clang::ParmVarDecl *param) { + return clazy::typeName(param->getType(), lo, simpleName) == typeName; }); } @@ -282,8 +300,8 @@ inline void dump(const clang::SourceManager &sm, clang::Stmt *s) if (!s) return; - llvm::errs() << "Start=" << s->getLocStart().printToString(sm) - << "; end=" << s->getLocStart().printToString(sm) + llvm::errs() << "Start=" << getLocStart(s).printToString(sm) + << "; end=" << getLocStart(s).printToString(sm) << "\n"; for (auto child : s->children()) |