Skip to content

Commit 3a3bef3

Browse files
committed
CPP: Add the new Japanese era.
1 parent bac39e6 commit 3a3bef3

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

cpp/ql/src/Best Practices/Magic Constants/JapaneseEraDate.ql

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,31 @@ predicate assignedDay(Struct s, DayFieldAccess day, int value) {
3636
)
3737
}
3838

39+
predicate eraDate(int year, int month, int day) {
40+
year = 1989 and month = 1 and day = 8
41+
or
42+
year = 2019 and month = 5 and day = 1
43+
}
44+
45+
3946
predicate badStructInitialization(Element target, string message) {
40-
exists(StructLikeClass s, YearFieldAccess year, MonthFieldAccess month, DayFieldAccess day |
41-
assignedYear(s, year, 1989) and
42-
assignedMonth(s, month, 1) and
43-
assignedDay(s, day, 8) and
47+
exists(
48+
StructLikeClass s, YearFieldAccess year, MonthFieldAccess month, DayFieldAccess day,
49+
int yearValue, int monthValue, int dayValue
50+
|
51+
eraDate(yearValue, monthValue, dayValue) and
52+
assignedYear(s, year, yearValue) and
53+
assignedMonth(s, month, monthValue) and
54+
assignedDay(s, day, dayValue) and
4455
target = year and
4556
message = "A time struct that is initialized with exact Japanese calendar era start date."
4657
)
4758
}
4859

4960
predicate badCall(Element target, string message) {
5061
exists(Call cc, int i |
51-
cc.getArgument(i).getValue().toInt() = 1989 and
52-
cc.getArgument(i + 1).getValue().toInt() = 1 and
53-
cc.getArgument(i + 2).getValue().toInt() = 8 and
62+
eraDate(cc.getArgument(i).getValue().toInt(), cc.getArgument(i + 1).getValue().toInt(),
63+
cc.getArgument(i + 2).getValue().toInt()) and
5464
target = cc and
5565
message = "Call that appears to have hard-coded Japanese era start date as parameter."
5666
)

cpp/ql/test/query-tests/Best Practices/Magic Constants/Japanese Era/ConstructorOrMethodWithExactDate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ int Main()
3838
// GOOD: method call with the same parameters in a different order (we only track year, month, day)
3939
EraInfo * pDateTimeUtil4 = EraInfo::EraInfoFromDate(1, 2, 8, 1, 1989, L"\u5e73\u6210");
4040

41-
// BAD: constructor creating a EraInfo with exact Reiwa era start date [NOT DETECTED]
41+
// BAD: constructor creating a EraInfo with exact Reiwa era start date
4242
EraInfo * pDateTimeUtil5 = new EraInfo(2019, 5, 1);
4343
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
| ConstructorOrMethodWithExactDate.cpp:27:31:27:53 | call to EraInfo | Call that appears to have hard-coded Japanese era start date as parameter. |
22
| ConstructorOrMethodWithExactDate.cpp:30:32:30:77 | call to EraInfo | Call that appears to have hard-coded Japanese era start date as parameter. |
33
| ConstructorOrMethodWithExactDate.cpp:36:32:36:55 | call to EraInfoFromDate | Call that appears to have hard-coded Japanese era start date as parameter. |
4+
| ConstructorOrMethodWithExactDate.cpp:42:32:42:54 | call to EraInfo | Call that appears to have hard-coded Japanese era start date as parameter. |
45
| StructWithExactDate.cpp:31:13:31:19 | tm_year | A time struct that is initialized with exact Japanese calendar era start date. |
56
| StructWithExactDate.cpp:46:8:46:12 | wYear | A time struct that is initialized with exact Japanese calendar era start date. |
7+
| StructWithExactDate.cpp:60:9:60:13 | wYear | A time struct that is initialized with exact Japanese calendar era start date. |

cpp/ql/test/query-tests/Best Practices/Magic Constants/Japanese Era/StructWithExactDate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ int main()
5353
st1.wYear = 1990;
5454

5555

56-
// BAD: Creation of SYSTEMTIME stuct corresponding to the beginning of Reiwa era [NOT DETECTED]
56+
// BAD: Creation of SYSTEMTIME stuct corresponding to the beginning of Reiwa era
5757
SYSTEMTIME st2;
5858
st2.wDay = 1;
5959
st2.wMonth = 5;

0 commit comments

Comments
 (0)