-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVariableAnalyser.cpp
More file actions
184 lines (171 loc) · 8 KB
/
Copy pathVariableAnalyser.cpp
File metadata and controls
184 lines (171 loc) · 8 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "header/VariableAnalyser.h"
#include <llvm/Support/CommandLine.h>
#include "header/HelperFunctions.h"
#include "header/OutputHandler.h"
#include "header/CodeMatcher.h"
#include <llvm/Support/CommandLine.h>
namespace variableanalysis {
class VariableAnalyser{
std::vector<VariableInstance> oldVariables;
std::vector<VariableInstance> newVariables;
std::vector<VariableInstance> unmatchedOldVariables;
OutputHandler* outputHandler;
int counter=0;
static int findVariable(const std::vector<VariableInstance>& set, const VariableInstance& variableInstance){
// TODO: check if there are more than one variable with the same name and if yes, use the filename as another identifer
// prioritylist (name has to be the same): qualifiedName > filename > closest match on location
std::map<int, VariableInstance> priorityMatches;
std::map<int, VariableInstance> secondaryMatches;
for(int i=0;i<set.size();i++){
if(set.at(i).qualifiedName == variableInstance.qualifiedName){
priorityMatches.insert(std::make_pair(i, set.at(i)));
if(set.at(i).equals(variableInstance)){
return i;
}
}else if(set.at(i).name == variableInstance.name){
secondaryMatches.insert(std::make_pair(i, set.at(i)));
}
}
for(const auto& possibleMatch: priorityMatches){
// return the first match with the same filename
if(possibleMatch.second.filename == variableInstance.filename){
return possibleMatch.first;
}
}
// TODO: now filechanges cant be recognized, but i dont have an idea how to do that because trying to include it had the effect of including way to many variables with the same name in different files
for(const auto& possibleMatch: secondaryMatches){
// return the first match with the same filename
if(possibleMatch.second.filename == variableInstance.filename){
return possibleMatch.first;
}
}
return -1;
}
public:
VariableAnalyser(const std::vector<VariableInstance>& oldVariables, const std::vector<VariableInstance>& newVariables, OutputHandler* outputHandler){
this->oldVariables = oldVariables;
this->newVariables = newVariables;
this->outputHandler = outputHandler;
}
void compareVariables(){
for (const auto &oldVar: oldVariables){
if(oldVar.accessSpecifier == "private" || oldVar.accessSpecifier == "protected"){
continue;
}
auto indexInNew = findVariable(newVariables, oldVar);
if(indexInNew != -1){
VariableInstance newVar = newVariables.at(indexInNew);
if(compareLocation(oldVar, newVar, true)){
unmatchedOldVariables.push_back(oldVar);
continue;
}
outputHandler->initialiseVariableInstance(oldVar);
compareMainHeader(oldVar, newVar);
compareLocation(oldVar, newVar, false);
compareQualifiers(oldVar, newVar);
newVariables.erase(newVariables.begin() + indexInNew);
}else{
outputHandler->initialiseVariableInstance(oldVar);
outputHandler->outputVariableDeleted(oldVar);
}
outputHandler->endOfCurrentVariable();
counter++;
}
for (const auto &oldVar: unmatchedOldVariables){
auto indexInNew = findVariable(newVariables, oldVar);
if(indexInNew != -1){
VariableInstance newVar = newVariables.at(indexInNew);
outputHandler->initialiseVariableInstance(oldVar);
compareMainHeader(oldVar, newVar);
compareLocation(oldVar, newVar, false);
compareQualifiers(oldVar, newVar);
newVariables.erase(newVariables.begin() + indexInNew);
}else{
outputHandler->initialiseVariableInstance(oldVar);
outputHandler->outputVariableDeleted(oldVar);
}
outputHandler->endOfCurrentVariable();
counter++;
}
}
private:
void compareMainHeader(const VariableInstance& oldVar, const VariableInstance& newVar){
if(oldVar.isClassMember != newVar.isClassMember){
outputHandler->outputVariableClassMember(oldVar, newVar);
}
if(oldVar.type != newVar.type){
outputHandler->outputVariableTypeChange(oldVar, newVar);
}
if(oldVar.filename != newVar.filename){
outputHandler->outputVariableFileChange(oldVar, newVar);
}
if(oldVar.storageClass != newVar.storageClass){
outputHandler->outputVariableStorageClassChange(oldVar, newVar);
}
}
bool compareLocation(const VariableInstance& oldVar, const VariableInstance& newVar, bool internalCheck = false){
if(oldVar.location.size() != newVar.location.size()){
if(!internalCheck) outputHandler->outputVariableLocationChange(oldVar, newVar);
return true;
}else{
for(int i=0;i<oldVar.location.size();i++){
if(oldVar.location.at(i) != newVar.location.at(i)){
if(!internalCheck) outputHandler->outputVariableLocationChange(oldVar, newVar);
return true;
}
}
}
return false;
}
void compareQualifiers(const VariableInstance& oldVar, const VariableInstance& newVar){
// processed in detail, in case there is more complicated logic introduced in the future
if(oldVar.isInline != newVar.isInline){
outputHandler->outputVariableInlineChange(oldVar, newVar);
}
if(oldVar.accessSpecifier != newVar.accessSpecifier){
outputHandler->outputVariableAccessSpecifierChange(oldVar, newVar);
}
if(oldVar.isConst != newVar.isConst){
outputHandler->outputVariableConstChange(oldVar, newVar);
}
if(oldVar.isExplicit != newVar.isExplicit){
outputHandler->outputVariableExplicitChange(oldVar, newVar);
}
if(oldVar.isVolatile != newVar.isVolatile){
outputHandler->outputVariableVolatileChange(oldVar, newVar);
}
if(oldVar.isMutable != newVar.isMutable){
outputHandler->outputVariableMutableChange(oldVar, newVar);
}
}
/*
void compareDefinitions(const VariableInstance& oldVar, const VariableInstance& newVar){
for (const auto &oldDef: oldVar.definitions){
bool found = false;
for (const auto &newDef: newVar.definitions){
if(oldDef.filename == newDef.filename){
found = true;
if(oldDef.defaultValue != newDef.defaultValue){
outputHandler->outputVariableDefaultValueChange(oldDef, newDef);
}
}
}
if(!found){
outputHandler->outputVariableDefinitionDeleted(oldDef);
}
}
for (const auto &newDef: newVar.definitions){
bool found = false;
for (const auto &oldDef: oldVar.definitions){
if(oldDef.filename == newDef.filename){
found = true;
}
}
if(!found){
outputHandler->outputVariableDefinitionAdded(newDef);
}
}
}
*/
};
}