-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmusic_utility.cpp
1797 lines (1541 loc) · 50.2 KB
/
music_utility.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include "music_utility.h"
#include "clang/AST/PrettyPrinter.h"
set<string> arithemtic_operators{"+", "-", "*", "/", "%"};
set<string> bitwise_operators{"&", "|", "^"};
set<string> shift_operators{">>", "<<"};
set<string> logical_operators{"&&", "||"};
set<string> relational_operators{">", "<", "<=", ">=", "==", "!="};
set<string> arith_assignment_operators{"+=", "-=", "*=", "/=", "%="};
set<string> bitwise_assignment_operators{"&=", "|=", "^="};
set<string> shift_assignment_operators{"<<=", ">>="};
set<string> assignment_operator{"="};
string ConvertToString(Stmt *from, LangOptions &LangOpts)
{
string SStr;
llvm::raw_string_ostream S(SStr);
from->printPretty(S, 0, PrintingPolicy(LangOpts));
return S.str();
}
// Print out each element of a string set in a single line.
void PrintStringSet(std::set<std::string> &string_set)
{
for (auto element: string_set)
{
std::cout << "//" << element;
}
std::cout << "//\n";
}
// Print out each elemet of a string vector in a single line.
void PrintStringVector(const vector<string> &string_vector)
{
for (auto it: string_vector)
std::cout << "//" << it;
std::cout << "//\n";
}
// remove spaces at the beginning and end_loc of string str
string TrimBeginningAndEndingWhitespace(const string &str)
{
string whitespace{" "};
auto first_non_whitespace_index = str.find_first_not_of(whitespace);
if (first_non_whitespace_index == string::npos)
return ""; // no content
auto last_non_whitespace_index = str.find_last_not_of(whitespace);
auto string_range = last_non_whitespace_index - first_non_whitespace_index + 1;
return str.substr(first_non_whitespace_index, string_range);
}
// Divide string into elements of a string set with delimiter
void SplitStringIntoSet(string target, set<string> &out_set,
const string delimiter)
{
size_t pos = 0;
string token;
while ((pos = target.find(delimiter)) != string::npos) {
token = target.substr(0, pos);
out_set.insert(TrimBeginningAndEndingWhitespace(token));
target.erase(0, pos + delimiter.length());
}
out_set.insert(TrimBeginningAndEndingWhitespace(target));
}
void SplitStringIntoVector(string target, vector<string> &out_vector,
const string delimiter)
{
size_t pos = 0;
string token;
while ((pos = target.find(delimiter)) != string::npos) {
token = target.substr(0, pos);
out_vector.push_back(token);
target.erase(0, pos + delimiter.length());
}
out_vector.push_back(target);
}
/**
@param hexa: hexa string of the following form "'\xF...F'"
@return string of the integer value of the given hexa string
*/
string ConvertHexaStringToIntString(const string hexa_str)
{
char first_hexa_digit = hexa_str.at(3);
if (!((first_hexa_digit >= '0' && first_hexa_digit <= '9') ||
(first_hexa_digit >= 'a' && first_hexa_digit <= 'f') ||
(first_hexa_digit >= 'A' && first_hexa_digit <= 'F')))
return hexa_str;
char secondh_hexa_digit = hexa_str.at(4);
if (secondh_hexa_digit != '\'')
if (!((secondh_hexa_digit >= '0' && secondh_hexa_digit <= '9') ||
(secondh_hexa_digit >= 'a' && secondh_hexa_digit <= 'f') ||
(secondh_hexa_digit >= 'A' && secondh_hexa_digit <= 'F')))
return hexa_str;
return to_string(stoul(hexa_str.substr(3, hexa_str.length() - 4), nullptr, 16));
}
string ConvertCharStringToIntString(const string s)
{
// it is a single, non-escaped character like 'a'
if (s.length() == 3)
return to_string(int(s.at(1)));
if (s.at(1) == '\\')
{
// it is an escaped character like '\n'
int length = s.length() - 3;
switch (s.at(2))
{
case 'a':
if (length == 1)
return to_string(int('\a'));
break;
case 'b':
if (length == 1)
return to_string(int('\b'));
break;
case 'f':
if (length == 1)
return to_string(int('\f'));
break;
case 'n':
if (length == 1)
return to_string(int('\n'));
break;
case 'r':
if (length == 1)
return to_string(int('\r'));
break;
case 't':
if (length == 1)
return to_string(int('\t'));
break;
case 'v':
if (length == 1)
return to_string(int('\v'));
break;
case '0':
if (length == 1)
return to_string(int('\0'));
break;
case '\\':
case '\'':
case '\"':
if (length == 1)
return to_string(int(s.at(2)));
break;
case 'x':
case 'X':
if (length <= 3 && length > 1)
return ConvertHexaStringToIntString(s);
else
return s; // hexadecimal value higher than FF. not a char
default:
cout << "cannot convert " << s << " to string of int\n";
return s;
}
}
// the function does not handle cases like 'abc'
cout << "cannot convert " << s << " to string of int\n";
return s;
}
bool IsStringElementOfVector(string s, vector<string> &string_vector)
{
auto it = string_vector.begin();
while (it != string_vector.end() && (*it).compare(s) != 0)
++it;
// if the iterator reach the end_loc then the string is NOT in vector v
return !(it == string_vector.end());
}
bool IsStringElementOfSet(string s, set<string> &string_set)
{
auto it = string_set.begin();
while (it != string_set.end() && (*it).compare(s) != 0)
++it;
return it != string_set.end();
}
bool ConvertStringToInt(string s, int &n)
{
stringstream convert(s);
if (!(convert >> n))
return false;
return true;
}
/**
@param s string literal from C input file
pos index at which to check if the character there is a whitespace
@return True if the character at pos is a whitespace
False otherwise
If the character at pos is whitespace, pos is changed to the
position after the whitespace
*/
bool IsWhitespace(const string &s, int &pos)
{
if (s[pos] == ' ')
{
++pos;
return true;
}
if (s[pos] == '\\')
{
if (s[pos+1] == 'f' || s[pos+1] == 'n' ||
s[pos+1] == 'r' || s[pos+1] == 't' ||
s[pos+1] == 'v')
{
pos += 2;
return true;
}
if (pos + 3 < s.length())
{
string temp{s.substr(pos,4)};
if (temp.compare("\\011") == 0 ||
temp.compare("\\012") == 0 ||
temp.compare("\\013") == 0 ||
temp.compare("\\014") == 0 ||
temp.compare("\\015") == 0)
{
pos += 4;
return true;
}
}
}
return false;
}
bool IsCharAlphabetic(char c)
{
return (c >= 65 && c <= 90) || (c >= 97 && c <= 122);
}
bool IsCharNumeric(char c)
{
return c >= '0' && c <= '9';
}
bool IsValidVariableName(string s)
{
// First char has to be alphabetic or underscore
// String should not be empty
if ((!IsCharAlphabetic(s[0]) && s[0] != '_') ||
s.length() < 1)
return false;
// Variable name can only consists of alphabet, number and underscore.
for (char c: s)
if (!(IsCharAlphabetic(c) || IsCharNumeric(c) || c == '_'))
return false;
return true;
}
/**
@param s string literal from input file
@return index of the first non whitespace character (space, \f, \n, \r, \t, \v)
Return an int higher than 1
*/
int GetFirstNonWhitespaceIndex(const string &s)
{
// Skip the first character which is double quote
int ret{1};
while (IsWhitespace(s, ret)) {}
return ret;
}
int GetLastNonWhitespaceIndex(const string &s)
{
int ret{GetFirstNonWhitespaceIndex(s)};
int i = ret;
int length{(int) s.length()};
while (i != length-1)
{
// If the character is not whitespace, change ret position.
if (!IsWhitespace(s, i))
{
// i is not changed because this character is NOT whitespace
if (s[i] == '\\') // escaped character
i += 2;
else
++i;
ret = i - 1;
}
}
return ret;
}
bool NumIsFloat(std::string num)
{
for (int i = 0; i < num.length(); ++i)
{
if (num[i] == '.')
return true;
}
return false;
}
/**
Check whether a string is a number (int or real)
@param target targeted string
@return true if it is a number
false otherwise
*/
bool StringIsANumber(std::string target)
{
bool no_dot = true;
for (int i = 0; i < target.length(); ++i)
{
//-------add test of string length to limit number
if (isdigit(target[i]))
continue;
// the dash cannot only appear at the first position
// and there must be numbers behind it
if (target[i] == '-' && i == 0 && target.length() > 1)
continue;
// prevent cases of 2 dot in a number
if (target[i] == '.' && no_dot)
{
no_dot = false;
continue;
}
return false;
}
return true;
}
/**
Check whether the domain of an operator specified by user is correct
@param mutant_name name of the operator
domain string set containing targeted tokens
*/
void ValidateDomainOfMutantOperator(std::string &mutant_name,
std::set<std::string> &domain)
{
// set of operators mutating logical operator
std::set<std::string> logical{/*"OLLN",*/ /*"OLAN",*/ /*"OLBN",*/ /*"OLRN",*/ /*"OLSN"*/};
// set of operators mutating relational operator
std::set<std::string> relational{/*"ORLN",*/ /*"ORAN",*/ /*"ORBN",*/ /*"ORRN",*/ /*"ORSN"*/};
// set of operators mutating arithmetic operator
std::set<std::string> arithmetic{/*"OALN",*/ /*"OAAN",*/ /*"OABN",*/ /*"OARN",*/ /*"OASN"*/};
// set of operators mutating bitwise operator
std::set<std::string> bitwise{/*"OBLN",*/ /*"OBAN",*/ /*"OBBN",*/ /*"OBRN",*/ /*"OBSN"*/};
// set of operators mutating shift operator
std::set<std::string> shift{/*"OSLN"*/ /*"OSAN",*/ /*"OSBN"*//*, "OSRN"*//*, "OSSN"*/};
// set of operators mutating arithmetic assignment operator
std::set<std::string> arithmetic_assignment{/*"OAAA",*/ /*"OABA",*/ /*"OAEA",*/ /*"OASA"*/};
// set of operators mutating bitwise assignment operator
std::set<std::string> bitwise_assignment{/*"OBAA",*/ /*"OBBA",*/ /*"OBEA",*/ /*"OBSA"*/};
// set of operators mutating shift assignment operator
std::set<std::string> shift_assignment{/*"OSAA", "OSBA", "OSEA", "OSSA"*/};
// set of operators mutating plain assignment operator
std::set<std::string> plain_assignment{/*"OEAA",*/ /*"OEBA",*/ /*"OESA"*/};
// set of operators whose domain cannot be altered.
std::set<std::string> empty_domain{/*"CRCR", "SSDL",*/ /*"VLSR",*/ /*"VGSR",*/ /*"VGAR",*/
/*"VLAR",*/ /*"VGTR",*/ /*"VLTR",*/ /*"VGPR",*/ /*"VLPR",*/
/*"VSCR",*/ /*"CGCR",*/ /*"CLCR",*/ /*"CGSR",*/ /*"CLSR",*/
/*"OMMO",*/ /*"OPPO",*/ /*"OLNG",*/ /*"OCNG",*/ /*"OBNG",*/
"OBOM", /*"VTWD",*/ /*"OIPM",*/ /*"OCOR",*/ /*"SANL",*/
/*"SRWS",*/ /*"SCSR",*/ /*"VLSF",*/ /*"VGSF",*/ /*"VGTF",*/
/*"VLTF",*/ /*"VGPF"*//*, "VLPF"*//*, "VTWF"*/};
// Determine the set of valid tokens based on the type of operator
std::set<std::string> valid_domain;
if (logical.find(mutant_name) != logical.end())
{
valid_domain = {"&&", "||"};
}
else if (relational.find(mutant_name) != relational.end())
{
valid_domain = {">", "<", "<=", ">=", "==", "!="};
}
else if (arithmetic.find(mutant_name) != arithmetic.end())
{
valid_domain = {"+", "-", "*", "/", "%"};
}
else if (bitwise.find(mutant_name) != bitwise.end())
{
valid_domain = {"&", "|", "^"};
}
else if (shift.find(mutant_name) != shift.end())
{
valid_domain = {"<<", ">>"};
}
else if (arithmetic_assignment.find(mutant_name) != arithmetic_assignment.end())
{
valid_domain = {"+=", "-=", "*=", "/=", "%="};
}
else if (bitwise_assignment.find(mutant_name) != bitwise_assignment.end())
{
valid_domain = {"&=", "|=", "^="};
}
else if (shift_assignment.find(mutant_name) != shift_assignment.end())
{
valid_domain = {"<<=", ">>="};
}
else if (plain_assignment.find(mutant_name) != plain_assignment.end())
{
valid_domain = {"="};
}
else if (empty_domain.find(mutant_name) != empty_domain.end())
{
if (!domain.empty())
{
domain.clear();
std::cout << "-A options are not available for " << mutant_name << std::endl;
exit(1);
}
return;
}
else
{
std::cout << "Cannot identify mutant operator " << mutant_name << std::endl;
// exit(1);
return;
}
// if domain is not empty, then check if each element in the set is valid
if (domain.size() > 0)
{
for (auto it = domain.begin(); it != domain.end();)
{
if ((*it).empty())
it = domain.erase(it);
else if (valid_domain.find(*it) == valid_domain.end())
{
std::cout << "Mutant operator " << mutant_name << " cannot mutate " << *it << std::endl;
exit(1);
// it = domain.erase(it);
}
else
++it;
}
}
else
{
// user did not specify domain for this operator
// So mutate every valid token for this operator.
domain = valid_domain;
}
}
/**
Check whether the range of an operator specified by user is correct
@param mutant_name name of the operator
range string set containing tokens to mutate with
*/
void ValidateRangeOfMutantOperator(std::string &mutant_name,
std::set<std::string> &range)
{
// set of operators mutating to a logical operator
std::set<std::string> logical{/*"OALN",*/ /*"OBLN",*/ /*"OLLN",*/ /*"ORLN",*/ /*"OSLN"*/};
// set of operators mutating to a relational operator
std::set<std::string> relational{/*"OLRN",*/ /*"OARN",*/ /*"OBRN",*/ /*"ORRN",*/ /*"OSRN"*/};
// set of operators mutating to a arithmetic operator
std::set<std::string> arithmetic{/*"OLAN",*/ /*"OAAN",*/ /*"OBAN",*/ /*"ORAN",*/ /*"OSAN"*/};
// set of operators mutating to a arithmetic operator
std::set<std::string> bitwise{/*"OLBN",*/ /*"OABN",*/ /*"OBBN",*/ /*"ORBN",*/ /*"OSBN"*/};
// set of operators mutating to a arithmetic operator
std::set<std::string> shift{/*"OLSN", "OASN", "OBSN",*/ /*"ORSN"*//*, "OSSN"*/};
// set of operators mutating to arithmetic assignment operator
std::set<std::string> arithmetic_assignment{/*"OAAA",*/ /*"OBAA",*/ /*"OEAA"*//*, "OSAA"*/};
// set of operators mutating to bitwise assignment operator
std::set<std::string> bitwise_assignment{/*"OABA",*/ /*"OBBA",*/ /*"OEBA"*//*, "OSBA"*/};
// set of operators mutating to shift assignment operator
std::set<std::string> shift_assignment{/*"OASA", "OBSA",*/ /*"OESA"*//*, "OSSA"*/};
// set of operators mutating to plain assignment operator
std::set<std::string> plain_assignment{/*"OAEA",*/ /*"OBEA",*/ /*"OSEA"*/};
// set of operators whose range cannot be altered.
std::set<std::string> empty_range{/*"SSDL",*/ /*"VLSR",*/ /*"VGSR",*/ /*"VGAR",*/ /*"VLAR",*/
/*"VGTR",*/ /*"VLTR",*/ /*"VGPR",*/ /*"VLPR",*/ /*"VSCR",*/
/*"CGCR",*/ /*"CLCR",*/ /*"CGSR",*/ /*"CLSR",*/ /*"OMMO", */
/*"OPPO",*/ /*"OLNG",*/ /*"OCNG",*/ /*"OBNG",*/ "OBOM",
/*"VTWD",*/ /*"OIPM",*/ /*"OCOR",*/ /*"SANL",*/ /*"SRWS",*/
/*"SCSR",*/ /*"VLSF",*/ /*"VGSF",*/ /*"VGTF",*/ /*"VLTF",*/
/*"VGPF"*//*, "VLPF"*//*, "VTWF"*/};
// Determine the set of valid tokens based on the type of operator
std::set<std::string> valid_range;
if (logical.find(mutant_name) != logical.end())
{
valid_range = {"&&", "||"};
}
else if (relational.find(mutant_name) != relational.end())
{
valid_range = {">", "<", "<=", ">=", "==", "!="};
}
else if (arithmetic.find(mutant_name) != arithmetic.end())
{
valid_range = {"+", "-", "*", "/", "%"};
}
else if (bitwise.find(mutant_name) != bitwise.end())
{
valid_range = {"&", "|", "^"};
}
else if (shift.find(mutant_name) != shift.end())
{
valid_range = {"<<", ">>"};
}
else if (arithmetic_assignment.find(mutant_name) != arithmetic_assignment.end())
{
valid_range = {"+=", "-=", "*=", "/=", "%="};
}
else if (bitwise_assignment.find(mutant_name) != bitwise_assignment.end())
{
valid_range = {"&=", "|=", "^="};
}
else if (shift_assignment.find(mutant_name) != shift_assignment.end())
{
valid_range = {"<<=", ">>="};
}
else if (plain_assignment.find(mutant_name) != plain_assignment.end())
{
valid_range = {"="};
}
/*else if (mutant_name.compare("CRCR") == 0)
{
if (range.size() > 0)
{
for (auto num = range.begin(); num != range.end(); )
{
// if user input something not a number, it is ignored. (delete from the set)
if (!StringIsANumber(*num))
{
std::cout << "Warning: " << *num << " is not a number\n";
exit(1);
// num = range.erase(num);
}
else
++num;
}
}
return;
}*/
else if (empty_range.find(mutant_name) != empty_range.end())
{
if (!range.empty())
{
range.clear();
std::cout << "-B options are not available for " << mutant_name << std::endl;
exit(1);
}
return;
}
else
{
std::cout << "Cannot identify mutant operator " << mutant_name << std::endl;
// exit(1);
return;
}
// if range is not empty, then check if each element in the set is valid
if (range.size() > 0) {
for (auto it = range.begin(); it != range.end();)
{
if ((*it).empty())
it = range.erase(it);
else if (valid_range.find(*it) == valid_range.end())
{
std::cout << "Mutant operator " << mutant_name << " cannot mutate to " << *it << std::endl;
exit(1);
// it = range.erase(it);
}
else
++it;
}
}
else {
// user did not specify range for this operator
// So mutate to every valid token for this operator.
range = valid_range;
}
}
int GetLineNumber(SourceManager &src_mgr, SourceLocation loc)
{
return static_cast<int>(src_mgr.getExpansionLineNumber(loc));
}
int GetColumnNumber(SourceManager &src_mgr, SourceLocation loc)
{
return static_cast<int>(src_mgr.getExpansionColumnNumber(loc));
}
SourceLocation TryGetEndLocAfterBracketOrSemicolon(SourceLocation loc,
CompilerInstance *comp_inst)
{
SourceLocation ret{};
SourceManager &src_mgr = comp_inst->getSourceManager();
if (*(src_mgr.getCharacterData(loc)) == '}' ||
*(src_mgr.getCharacterData(loc)) == ';' ||
*(src_mgr.getCharacterData(loc)) == ']' ||
*(src_mgr.getCharacterData(loc)) == ')')
ret = loc.getLocWithOffset(1);
else
ret = clang::Lexer::findLocationAfterToken(loc,
tok::semi, src_mgr,
comp_inst->getLangOpts(),
false);
return ret;
}
void PrintLocation(SourceManager &src_mgr, SourceLocation loc)
{
cout << "(" << GetLineNumber(src_mgr, loc) << " : ";
cout << GetColumnNumber(src_mgr, loc) << ")" << endl;
}
void PrintRange(SourceManager &src_mgr, SourceRange range)
{
cout << "this range is from ";
PrintLocation(src_mgr, range.getBegin());
cout << " to ";
PrintLocation(src_mgr, range.getEnd());
}
// Return the number of not-newline character
int CountNonNewlineChar(const string &s)
{
int res = 0;
for (int i = 0; i < s.length(); ++i)
{
if (s[i] != '\n')
++res;
}
return res;
}
// Return True if location loc appears before range.
// (location 1,1 appears before every other valid locations)
bool LocationBeforeRangeStart(SourceLocation loc, SourceRange range)
{
SourceLocation start_of_range = range.getBegin();
return loc < start_of_range;
}
// Return True if location loc appears after range.
// (EOF appears after every other valid locations)
bool LocationAfterRangeEnd(SourceLocation loc, SourceRange range)
{
SourceLocation end_of_range = range.getEnd();
return loc != end_of_range && !(loc < end_of_range);
}
// Return True if the location is inside the range (inclusive)
// Return False otherwise
bool LocationIsInRange(SourceLocation loc, SourceRange range)
{
return !LocationBeforeRangeStart(loc, range) &&
!LocationAfterRangeEnd(loc, range);
}
bool Range1IsPartOfRange2(SourceRange range1, SourceRange range2)
{
return LocationIsInRange(range1.getBegin(), range2) &&
LocationIsInRange(range1.getEnd(), range2);
}
int CountNonNullStmtInCompoundStmt(CompoundStmt *c)
{
int res{0};
for (CompoundStmt::body_iterator it = c->body_begin(); it != c->body_end(); ++it)
{
if (!isa<NullStmt>(*it))
++res;
}
return res;
}
/**
This function assumes the given location is either before or after
a semicolon (;). Though I can use a while loop to go back and forth
until a semicolon is found, multi stmt on a single line can be
confusing
@param loc: target location with previous assumption
@return location after semicolon
if cannot find then return given location
*/
SourceLocation GetLocationAfterSemicolon(SourceManager &src_mgr,
SourceLocation loc)
{
// cout << "cp GetLocationAfterSemicolon\n";
// PrintLocation(src_mgr, loc);
if (loc.isInvalid() || GetColumnNumber(src_mgr, loc) == 1 ||
GetLineNumber(src_mgr, loc) == 0)
return loc;
// cout << "passed\n";
SourceLocation previous_loc = src_mgr.translateLineCol(
src_mgr.getMainFileID(),
GetLineNumber(src_mgr, loc),
GetColumnNumber(src_mgr, loc) - 1);
// cout << "done translate\n";
if (*(src_mgr.getCharacterData(previous_loc)) == ';')
return loc;
// cout << "cp1\n";
if (*(src_mgr.getCharacterData(loc)) == ';')
return loc.getLocWithOffset(1);
// cout << "cp2\n";
return loc;
}
bool ExprIsPointer(Expr *e)
{
return e->getType().getCanonicalType().getTypePtr()->isPointerType();
}
bool ExprIsScalar(Expr *e)
{
//This additional check ensures that the expression is not null
//which will cause a core dump error that results in failure in
//making mutants on jsoncpp/gflags.
if(!((e->getType()).isNull())){
auto type = ((e->getType().getCanonicalType()).getTypePtr());
return type->isScalarType() && !ExprIsPointer(e);
}else {
return false;
}
// return type->isScalarType() && !ExprIsPointer(e) && !type->isClassType();
}
bool ExprIsFloat(Expr *e)
{
return ((e->getType()).getCanonicalType().getTypePtr())->isFloatingType();
}
bool ExprIsIntegral(CompilerInstance *comp_inst, Expr *e)
{
auto type = ((e->getType()).getCanonicalType().getTypePtr());
return type->isIntegralType(comp_inst->getASTContext());
}
bool ExprIsArray(Expr *e)
{
return ((e->getType().getCanonicalType()).getTypePtr())->isArrayType();
}
bool ExprIsStruct(Expr *e)
{
return e->getType().getCanonicalType().getTypePtr()->isStructureType();
}
bool ExprIsDeclRefExpr(Expr *e)
{
// An Enum value is not a declaration reference
if (DeclRefExpr *dre = dyn_cast<DeclRefExpr>(e))
{
if (isa<EnumConstantDecl>(dre->getDecl()))
return false;
return true;
}
return false;
}
bool ExprIsPointerDereferenceExpr(Expr *e)
{
if (UnaryOperator *uo = dyn_cast<UnaryOperator>(e))
{
if (uo->getOpcode() == UO_Deref)
return true;
return false;
}
return false;
}
bool ExprIsScalarReference(Expr *e)
{
if (ExprIsDeclRefExpr(e) || ExprIsPointerDereferenceExpr(e) ||
isa<ArraySubscriptExpr>(e) || isa<MemberExpr>(e))
if (ExprIsScalar(e))
return true;
return false;
}
bool ExprIsArrayReference(Expr *e)
{
if (ExprIsDeclRefExpr(e) || ExprIsPointerDereferenceExpr(e) ||
isa<ArraySubscriptExpr>(e) || isa<MemberExpr>(e))
if (ExprIsArray(e))
return true;
return false;
}
bool ExprIsStructReference(Expr *e)
{
if (ExprIsDeclRefExpr(e) || ExprIsPointerDereferenceExpr(e) ||
isa<ArraySubscriptExpr>(e) || isa<MemberExpr>(e))
if (ExprIsStruct(e))
return true;
return false;
}
bool ExprIsPointerReference(Expr *e)
{
if (ExprIsDeclRefExpr(e) || ExprIsPointerDereferenceExpr(e) ||
isa<ArraySubscriptExpr>(e) || isa<MemberExpr>(e))
if (ExprIsPointer(e))
return true;
return false;
}
bool OperatorIsArithmeticAssignment(BinaryOperator *b)
{
return b->getOpcode() >= BO_MulAssign &&
b->getOpcode() <= BO_SubAssign;
}
bool OperatorIsShiftAssignment(BinaryOperator *b)
{
return b->getOpcode() == BO_ShlAssign &&
b->getOpcode() == BO_ShrAssign;
}
bool OperatorIsBitwiseAssignment(BinaryOperator *b)
{
return b->getOpcode() >= BO_AndAssign &&
b->getOpcode() <= BO_OrAssign;
}
/**
@param start_loc: start_loc location of the targeted string literal
@return end_loc location of string literal
*/
SourceLocation GetEndLocOfStringLiteral(
SourceManager &src_mgr, SourceLocation start_loc)
{
int line_num = GetLineNumber(src_mgr, start_loc);
int col_num = GetColumnNumber(src_mgr, start_loc) + 1;
if (line_num == 0 || col_num == 0)
return start_loc;
// cout << "cp GetEndLocOfStringLiteral\n";
// Get the location right AFTER the first double quote
SourceLocation ret = src_mgr.translateLineCol(src_mgr.getMainFileID(),
line_num, col_num);
while (*(src_mgr.getCharacterData(ret)) != '\"')
{
// if there is a backslash then skip the next character
if (*(src_mgr.getCharacterData(ret)) == '\\')
{
ret = ret.getLocWithOffset(1);
}
ret = ret.getLocWithOffset(1);
}
ret = ret.getLocWithOffset(1);
return ret;
}
/**
@param start_loc: start_loc location of the targeted literal
@return end_loc location of number, char literal
*/
SourceLocation GetEndLocOfConstantLiteral(
SourceManager &src_mgr, SourceLocation start_loc)
{
int line_num = GetLineNumber(src_mgr, start_loc);
int col_num = GetColumnNumber(src_mgr, start_loc);
if (line_num == 0 || col_num == 0)
return start_loc;
// cout << "cp GetEndLocOfConstantLiteral\n";
SourceLocation ret = src_mgr.translateLineCol(
src_mgr.getMainFileID(), line_num, col_num);
// a char starts and ends with a single quote
bool is_char_literal = false;
if (*(src_mgr.getCharacterData(ret)) == '\'')
is_char_literal = true;
if (is_char_literal)
{
ret = ret.getLocWithOffset(1);
while (*(src_mgr.getCharacterData(ret)) != '\'')
{
// if there is a backslash then skip the next character
if (*(src_mgr.getCharacterData(ret)) == '\\')
{
ret = ret.getLocWithOffset(1);
}
ret = ret.getLocWithOffset(1);
}
// End of while loop result in location right before a single quote
ret = ret.getLocWithOffset(1);
}
else // not char
{
// Here, I am assuming the appearance of these characters
// signals the end_loc of a number literal.
while (*(src_mgr.getCharacterData(ret)) != ' ' &&
*(src_mgr.getCharacterData(ret)) != ';' &&
*(src_mgr.getCharacterData(ret)) != '+' &&
*(src_mgr.getCharacterData(ret)) != '-' &&
*(src_mgr.getCharacterData(ret)) != '*' &&
*(src_mgr.getCharacterData(ret)) != '/' &&
*(src_mgr.getCharacterData(ret)) != '%' &&
*(src_mgr.getCharacterData(ret)) != ',' &&
*(src_mgr.getCharacterData(ret)) != ')' &&
*(src_mgr.getCharacterData(ret)) != ']' &&
*(src_mgr.getCharacterData(ret)) != '}' &&
*(src_mgr.getCharacterData(ret)) != '>' &&
*(src_mgr.getCharacterData(ret)) != '<' &&
*(src_mgr.getCharacterData(ret)) != '=' &&
*(src_mgr.getCharacterData(ret)) != '!' &&
*(src_mgr.getCharacterData(ret)) != '|' &&
*(src_mgr.getCharacterData(ret)) != '?' &&
*(src_mgr.getCharacterData(ret)) != '&' &&
*(src_mgr.getCharacterData(ret)) != '~' &&
*(src_mgr.getCharacterData(ret)) != '`' &&
*(src_mgr.getCharacterData(ret)) != ':' &&
*(src_mgr.getCharacterData(ret)) != '\n')
ret = ret.getLocWithOffset(1);
}
return ret;
}