JonasToth updated this revision to Diff 462838.
JonasToth added a comment.
- remove unproductive check-fixes line
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D130793/new/
https://reviews.llvm.org/D130793
Files:
clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp
clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-values.cpp
clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-values.cpp
Index: clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-values.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-values.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-values.cpp
@@ -526,18 +526,13 @@
// 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};
+
+ using intPtr = int*;
+ intPtr np_local1[2] = {nullptr, nullptr};
}
inline void *operator new(decltype(sizeof(void *)), void *p) { return p; }
@@ -908,41 +903,6 @@
sizeof(int[++N]);
}
-template <typename T>
-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 <typename T>
-struct SmallVector : SmallVectorBase<T> {};
-
-template <class T>
-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<const int *> p_local0;
- // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p_local0' of type 'SmallVector<const int *>' can be declared 'const'
- // CHECK-FIXES: SmallVector<const int *> const p_local0
- SmallVector<const int *> 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;
};
Index: clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-values.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-values.cpp
+++ 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 <typename T>
+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 <typename T>
+struct SmallVector : SmallVectorBase<T> {};
+
+template <class T>
+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<const int *> p_local0;
+ // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p_local0' of type 'SmallVector<const int *>' can be declared 'const'
+ // CHECK-FIXES: SmallVector<const int *> const p_local0
+ SmallVector<const int *> 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);
}
Index: clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp
+++ clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp
@@ -12,8 +12,6 @@
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
-#include <iostream>
-
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<ArrayType>(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.
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits