-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast_rewriting_functions.cpp
More file actions
73 lines (63 loc) · 2.72 KB
/
ast_rewriting_functions.cpp
File metadata and controls
73 lines (63 loc) · 2.72 KB
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
//
// ast_rewriting_functions.c
// ClangInstrumentationPlugin
//
// Created by Alasdair Morrison on 15/06/2013.
// Copyright (c) 2013 Alasdair Morrison. All rights reserved.
//
#include <stdio.h>
#include "clang/Lex/Lexer.h"
#include "clang/Basic/SourceManager.h"
#include "MyRecursiveASTVisitor.h"
void replaceAll(std::string& str, const std::string& from, const std::string& to) {
if(from.empty())
return;
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
}
}
int get_length_of_token_at_location(clang::SourceLocation Loc, Rewriter &rewriter) {
return clang::Lexer::MeasureTokenLength(
Loc, rewriter.getSourceMgr(), rewriter.getLangOpts());
}
bool replace_text_at_location(Rewriter &rewriter, clang::SourceLocation start, int end, const char* new_text) {
return rewriter.ReplaceText(start, end, new_text);
}
/*std::string get_location_to_string(Rewriter &rewriter, clang::SourceManager* sm, clang::SourceLocation start, clang::SourceLocation end) {
clang::SourceLocation b(start), _e(end);
clang::SourceLocation e(clang::Lexer::getLocForEndOfToken(_e, 0, *sm, rewriter.getLangOpts()));
return std::string(sm->getCharacterData(b),
sm->getCharacterData(e)-sm->getCharacterData(b));
}*/
std::string get_location_to_string(Rewriter &rewriter, clang::SourceManager* sm, clang::SourceLocation start, clang::SourceLocation end) {
//start.dump(*sm);
//clang::SourceLocation _begin(start.), _end(end.getLocEnd());
return std::string(sm->getCharacterData(start),
sm->getCharacterData(end));//-sm->getCharacterData(start));
}
std::string convert_decl_to_str(Rewriter &rewriter, clang::FunctionDecl *d, clang::SourceManager* sm) {
clang::SourceLocation b(d->getLocStart()), _e(d->getLocEnd());
clang::SourceLocation e(clang::Lexer::getLocForEndOfToken(_e, 0, *sm, rewriter.getLangOpts()));
return std::string(sm->getCharacterData(b),
sm->getCharacterData(e)-sm->getCharacterData(b));
}
std::string get_return_type_from_function_declaration(FunctionDecl *f) {
// Make a stab at determining return type
// Getting actual return type is trickier
QualType q = f->getResultType();
const Type *typ = q.getTypePtr();
std::string ret;
if (typ->isVoidType())
ret = "void";
else
if (typ->isIntegerType())
ret = "integer";
else
if (typ->isCharType())
ret = "char";
else
ret = "Other";
return ret;
}