diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp --- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp @@ -12,8 +12,6 @@ #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/ASTMatchers/ASTMatchers.h" -#include - using namespace clang::ast_matchers; namespace clang { @@ -132,6 +130,12 @@ VC = VariableCategory::Reference; if (Variable->getType()->isPointerType()) VC = VariableCategory::Pointer; + if (Variable->getType()->isArrayType()) { + if (const auto *ArrayT = dyn_cast(Variable->getType())) { + if (ArrayT->getElementType()->isPointerType()) + VC = VariableCategory::Pointer; + } + } // Each variable can only be in one category: Value, Pointer, Reference. // Analysis can be controlled for every category. diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-values.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-values.cpp --- a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-values.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-values.cpp @@ -10,4 +10,65 @@ double *p_local0 = &np_local0[1]; // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p_local0' of type 'double *' can be declared 'const' // CHECK-FIXES: double *const p_local0 + + using doublePtr = double*; + using doubleArray = double[15]; + doubleArray np_local1; + doublePtr p_local1 = &np_local1[0]; + // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p_local1' of type 'doublePtr' (aka 'double *') can be declared 'const' + // CHECK-FIXES: doublePtr const p_local1 +} + +void range_for() { + int np_local0[2] = {1, 2}; + int *p_local0[2] = {&np_local0[0], &np_local0[1]}; + // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p_local0' of type 'int *[2]' can be declared 'const' + // CHECK-FIXES: int *const p_local0[2] + for (const int *p_local1 : p_local0) { + // CHECK-MESSAGES: [[@LINE-1]]:8: warning: variable 'p_local1' of type 'const int *' can be declared 'const' + // CHECK-FIXES: for (const int *const p_local1 : p_local0) + } + + int *p_local2[2] = {nullptr, nullptr}; + // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p_local2' of type 'int *[2]' can be declared 'const' + // CHECK-FIXES: int *const p_local2[2] + for (const auto *con_ptr : p_local2) { + } + +} + +template +struct SmallVectorBase { + T data[4]; + void push_back(const T &el) {} + int size() const { return 4; } + T *begin() { return data; } + const T *begin() const { return data; } + T *end() { return data + 4; } + const T *end() const { return data + 4; } +}; + +template +struct SmallVector : SmallVectorBase {}; + +template +void EmitProtocolMethodList(T &&Methods) { + // Note: If the template is uninstantiated the analysis does not figure out, + // that p_local0 could be const. Not sure why, but probably bails because + // some expressions are type-dependent. + SmallVector p_local0; + // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p_local0' of type 'SmallVector' can be declared 'const' + // CHECK-FIXES: SmallVector const p_local0 + SmallVector np_local0; + for (const auto *I : Methods) { + if (I == nullptr) + np_local0.push_back(I); + } + p_local0.size(); +} +void instantiate() { + int *p_local0[4] = {nullptr, nullptr, nullptr, nullptr}; + // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p_local0' of type 'int *[4]' can be declared 'const' + // CHECK-FIXES: int *const p_local0[4] + EmitProtocolMethodList(p_local0); } diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-values.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-values.cpp --- a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-values.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-values.cpp @@ -526,18 +526,15 @@ // CHECK-FIXES: int const p_local1[2] for (const int &const_ref : p_local1) { } +} - int *p_local2[2] = {&np_local0[0], &np_local0[1]}; - // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p_local2' of type 'int *[2]' can be declared 'const' - // CHECK-FIXES: int *const p_local2[2] - for (const int *con_ptr : p_local2) { - } - - int *p_local3[2] = {nullptr, nullptr}; - // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p_local3' of type 'int *[2]' can be declared 'const' - // CHECK-FIXES: int *const p_local3[2] - for (const auto *con_ptr : p_local3) { - } +void arrays_of_pointers_are_ignored() { + int *np_local0[2] = {nullptr, nullptr}; + // CHECK-NOT-FIXES: int * const np_local0[2] + + using intPtr = int*; + intPtr np_local1[2] = {nullptr, nullptr}; + // CHECK-NOT-FIXES: intPtr const np_local1[2] } inline void *operator new(decltype(sizeof(void *)), void *p) { return p; } @@ -908,41 +905,6 @@ sizeof(int[++N]); } -template -struct SmallVectorBase { - T data[4]; - void push_back(const T &el) {} - int size() const { return 4; } - T *begin() { return data; } - const T *begin() const { return data; } - T *end() { return data + 4; } - const T *end() const { return data + 4; } -}; - -template -struct SmallVector : SmallVectorBase {}; - -template -void EmitProtocolMethodList(T &&Methods) { - // Note: If the template is uninstantiated the analysis does not figure out, - // that p_local0 could be const. Not sure why, but probably bails because - // some expressions are type-dependent. - SmallVector p_local0; - // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p_local0' of type 'SmallVector' can be declared 'const' - // CHECK-FIXES: SmallVector const p_local0 - SmallVector np_local0; - for (const auto *I : Methods) { - if (I == nullptr) - np_local0.push_back(I); - } - p_local0.size(); -} -void instantiate() { - int *p_local0[4] = {nullptr, nullptr, nullptr, nullptr}; - // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p_local0' of type 'int *[4]' can be declared 'const' - // CHECK-FIXES: int *const p_local0[4] - EmitProtocolMethodList(p_local0); -} struct base { int member; };