-
-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathStringUtils.h
156 lines (124 loc) · 4.34 KB
/
StringUtils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
This file is part of the clang-lazy static checker.
Copyright (C) 2015 Klarälvdalens Datakonsult AB, a KDAB Group company, [email protected]
Author: Sérgio Martins <[email protected]>
Copyright (C) 2015 Sergio Martins <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
As a special exception, permission is given to link this program
with any edition of Qt, and distribute the resulting executable,
without including the source code for Qt in the source distribution.
*/
#ifndef CLANG_LAZY_STRING_UTILS_H
#define CLANG_LAZY_STRING_UTILS_H
#include "checkmanager.h"
#include "Utils.h"
#include <clang/AST/ExprCXX.h>
#include <clang/AST/DeclCXX.h>
#include <string>
template <typename T>
inline std::string classNameFor(T *ctorDecl)
{
return "";
}
template <>
inline std::string classNameFor(clang::CXXConstructorDecl *ctorDecl)
{
return ctorDecl->getParent()->getNameAsString();
}
template <>
inline std::string classNameFor(clang::CXXMethodDecl *method)
{
return method->getParent()->getNameAsString();
}
template <>
inline std::string classNameFor(clang::CXXConstructExpr *expr)
{
return classNameFor(expr->getConstructor());
}
template <typename T>
inline bool isOfClass(T *node, const std::string &className)
{
return node && classNameFor<T>(node) == className;
}
namespace StringUtils {
inline bool functionIsOneOf(clang::FunctionDecl *func, const std::vector<std::string> &functionNames)
{
return func && std::find(functionNames.cbegin(), functionNames.cend(), func->getNameAsString()) != functionNames.cend();
}
inline void printLocation(const clang::SourceLocation &loc, bool newLine = true)
{
llvm::errs() << loc.printToString(*(CheckManager::instance()->m_sm));
if (newLine)
llvm::errs() << "\n";
}
inline void printRange(const clang::SourceRange &range, bool newLine = true)
{
printLocation(range.getBegin(), false);
llvm::errs() << "-";
printLocation(range.getEnd(), newLine);
}
inline void printLocation(const clang::Stmt *s, bool newLine = true)
{
if (s)
printLocation(s->getLocStart(), newLine);
}
inline void printLocation(clang::PresumedLoc loc, bool newLine = true)
{
llvm::errs() << loc.getFilename() << " " << loc.getLine() << ":" << loc.getColumn();
if (newLine)
llvm::errs() << "\n";
}
inline std::string qualifiedMethodName(clang::CXXMethodDecl *method)
{
// method->getQualifiedNameAsString() returns the name with template arguments, so do a little hack here
if (!method || !method->getParent())
return "";
return method->getParent()->getNameAsString() + "::" + method->getNameAsString();
}
inline void printParents(clang::ParentMap *map, clang::Stmt *s)
{
int level = 0;
llvm::errs() << (s ? s->getStmtClassName() : nullptr) << "\n";
while (clang::Stmt *parent = Utils::parent(map, s)) {
++level;
llvm::errs() << std::string(level, ' ') << parent->getStmtClassName() << "\n";
s = parent;
}
}
inline std::string accessString(clang::AccessSpecifier s)
{
switch (s)
{
case clang::AccessSpecifier::AS_public:
return "public";
case clang::AccessSpecifier::AS_private:
return "private";
case clang::AccessSpecifier::AS_protected:
return "protected";
case clang::AccessSpecifier::AS_none:
return {};
}
return {};
}
inline void dump(clang::Stmt *s)
{
if (!s)
return;
llvm::errs() << "Start=" << s->getLocStart().printToString(*CheckManager::instance()->m_sm)
<< "; end=" << s->getLocStart().printToString(*CheckManager::instance()->m_sm)
<< "\n";
for (auto it = s->child_begin(), e = s->child_end(); it != e; ++it)
dump(*it);
}
}
#endif