-
Notifications
You must be signed in to change notification settings - Fork 11
/
Utils.cpp
111 lines (90 loc) · 2.46 KB
/
Utils.cpp
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
#include "Utils.h"
#include "clang/AST/GlobalDecl.h"
#include "clang/Basic/LangOptions.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Lex/Lexer.h"
#include "llvm/BinaryFormat/Magic.h"
namespace clang {
class PPLexer : public Lexer {
public:
PPLexer(const LangOptions &LangOpts, llvm::StringRef Code)
: Lexer(SourceLocation(), LangOpts, Code.begin(), Code.begin(),
Code.end()) {}
bool inPPDirective() const { return ParsingPreprocessorDirective; }
bool AdvanceTo(clang::Token &Tok, clang::tok::TokenKind kind) {
while (!Lex(Tok)) {
if (Tok.is(kind))
return false;
}
return true;
}
bool Lex(clang::Token &Tok) {
bool ret = LexFromRawLexer(Tok);
if (inPPDirective()) {
if (Tok.is(tok::eod))
ParsingPreprocessorDirective = false;
} else {
if (Tok.is(tok::hash)) {
ParsingPreprocessorDirective = true;
}
}
return ret;
}
};
size_t getFileOffset(const clang::Token &Tok) {
return Tok.getLocation().getRawEncoding();
}
size_t getWrapPos(const clang::LangOptions &LangOpts, const std::string &Code) {
PPLexer Lex(LangOpts, Code);
Token token;
while (true) {
bool atEOF = Lex.Lex(token);
if (Lex.inPPDirective() || token.is(tok::eod)) {
if (atEOF)
break;
continue;
}
if (token.is(tok::eof)) {
return std::string::npos;
}
const tok::TokenKind kind = token.getKind();
if (kind == tok::raw_identifier) {
StringRef keyword(token.getRawIdentifier());
if (keyword.equals("using")) {
if (Lex.AdvanceTo(token, tok::semi)) {
return std::string::npos;
}
return getFileOffset(token) + 1;
}
}
return getFileOffset(token);
}
return std::string::npos;
}
bool isCCIntMain(clang::FunctionDecl *FD) {
if (!FD) {
return false;
}
if (!FD->getDeclName().isIdentifier()) {
return false;
}
return FD->getName().startswith("ccint_main");
}
bool isDynamicLibrary(llvm::StringRef Path) {
llvm::file_magic type;
std::error_code EC = identify_magic(Path, type);
if (EC) {
return false;
}
switch (type) {
default:
return false;
case llvm::file_magic::macho_fixed_virtual_memory_shared_lib:
case llvm::file_magic::macho_dynamically_linked_shared_lib:
case llvm::file_magic::macho_dynamically_linked_shared_lib_stub:
case llvm::file_magic::elf_shared_object:
case llvm::file_magic::pecoff_executable:
return true;
}
}
} // namespace clang