Skip to content

Commit eaf4260

Browse files
committed
add test; fix bugs
1 parent d9a50a6 commit eaf4260

File tree

6 files changed

+34
-9
lines changed

6 files changed

+34
-9
lines changed

clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,10 @@ void ConstCorrectnessCheck::check(const MatchFinder::MatchResult &Result) {
210210
registerScope(LocalScope, Result.Context);
211211
if (ScopesCache[LocalScope]->isPointeeMutated(Variable))
212212
return;
213-
auto Diag = diag(Variable->getBeginLoc(),
214-
"variable %0 of type %1 can be declared 'const'")
215-
<< Variable << VT;
213+
auto Diag =
214+
diag(Variable->getBeginLoc(),
215+
"pointee of variable %0 of type %1 can be declared 'const'")
216+
<< Variable << VT;
216217
if (IsNormalVariableInTemplate)
217218
TemplateDiagnosticsCache.insert(Variable->getBeginLoc());
218219
if (!CanBeFixIt)
@@ -241,14 +242,16 @@ void ConstCorrectnessCheck::check(const MatchFinder::MatchResult &Result) {
241242
if (WarnPointersAsValues && !VT.isConstQualified())
242243
CheckValue();
243244
if (WarnPointersAsPointers) {
244-
if (const auto *PT = dyn_cast<PointerType>(VT))
245+
if (const auto *PT = dyn_cast<PointerType>(VT)) {
245246
if (!PT->getPointeeType().isConstQualified())
246247
CheckPointee();
247-
if (const auto *AT = dyn_cast<ArrayType>(VT))
248+
}
249+
if (const auto *AT = dyn_cast<ArrayType>(VT)) {
248250
if (!AT->getElementType().isConstQualified()) {
249251
assert(AT->getElementType()->isPointerType());
250252
CheckPointee();
251253
}
254+
}
252255
}
253256
return;
254257
}

clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-allowed-types.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
// RUN: misc-const-correctness.AllowedTypes: '[Pp]ointer$;[Pp]tr$;[Rr]ef(erence)?$;qualified::Type;::fully::QualifiedType;ConstTemplate', \
44
// RUN: misc-const-correctness.TransformPointersAsValues: true, \
55
// RUN: misc-const-correctness.TransformReferences: true, \
6-
// RUN: misc-const-correctness.WarnPointersAsValues: true } \
6+
// RUN: misc-const-correctness.WarnPointersAsValues: true, \
7+
// RUN: misc-const-correctness.WarnPointersAsPointers: false } \
78
// RUN: }" -- -fno-delayed-template-parsing
89

910
struct SmartPointer {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %check_clang_tidy %s misc-const-correctness %t \
2+
// RUN: -config='{CheckOptions: {\
3+
// RUN: misc-const-correctness.AnalyzeValues: false,\
4+
// RUN: misc-const-correctness.AnalyzeReferences: false,\
5+
// RUN: misc-const-correctness.AnalyzePointers: true,\
6+
// RUN: misc-const-correctness.WarnPointersAsValues: true,\
7+
// RUN: misc-const-correctness.WarnPointersAsPointers: true,\
8+
// RUN: misc-const-correctness.TransformPointersAsValues: true,\
9+
// RUN: misc-const-correctness.TransformPointersAsPointers: true\
10+
// RUN: }}' \
11+
// RUN: -- -fno-delayed-template-parsing
12+
13+
void pointee_to_const() {
14+
int a[] = {1, 2};
15+
int *p_local0 = &a[0];
16+
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: pointee of variable 'p_local0' of type 'int *' can be declared 'const'
17+
// CHECK-MESSAGES: [[@LINE-2]]:3: warning: variable 'p_local0' of type 'int *' can be declared 'const'
18+
// CHECK-FIXES: int const*const p_local0 = &a[0];
19+
}

clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-pointers.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313
void pointee_to_const() {
1414
int a[] = {1, 2};
1515
int *p_local0 = &a[0];
16-
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p_local0' of type 'int *' can be declared 'const'
16+
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: pointee of variable 'p_local0' of type 'int *' can be declared 'const'
1717
// CHECK-FIXES: int const*p_local0
1818
p_local0 = &a[1];
1919
}
2020

2121
void array_of_pointer_to_const() {
2222
int a[] = {1, 2};
2323
int *p_local0[1] = {&a[0]};
24-
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p_local0' of type 'int *[1]' can be declared 'const'
24+
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: pointee of variable 'p_local0' of type 'int *[1]' can be declared 'const'
2525
// CHECK-FIXES: int const*p_local0[1]
2626
p_local0[0] = &a[1];
2727
}
@@ -30,7 +30,7 @@ template<class T>
3030
void template_fn() {
3131
T a[] = {1, 2};
3232
T *p_local0 = &a[0];
33-
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p_local0' of type 'char *' can be declared 'const'
33+
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: pointee of variable 'p_local0' of type 'char *' can be declared 'const'
3434
// CHECK-FIXES: T const*p_local0
3535
p_local0 = &a[1];
3636
}

clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-values.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// RUN: -config='{CheckOptions: \
33
// RUN: {misc-const-correctness.AnalyzeValues: true,\
44
// RUN: misc-const-correctness.WarnPointersAsValues: true,\
5+
// RUN: misc-const-correctness.WarnPointersAsPointers: false,\
56
// RUN: misc-const-correctness.TransformPointersAsValues: true}}' \
67
// RUN: -- -fno-delayed-template-parsing
78

clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-transform-pointer-as-values.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// RUN: -config='{CheckOptions: \
33
// RUN: {misc-const-correctness.AnalyzeValues: true,\
44
// RUN: misc-const-correctness.WarnPointersAsValues: true, \
5+
// RUN: misc-const-correctness.WarnPointersAsPointers: false, \
56
// RUN: misc-const-correctness.TransformPointersAsValues: true}\
67
// RUN: }' -- -fno-delayed-template-parsing
78

0 commit comments

Comments
 (0)