njames93 created this revision.
njames93 added reviewers: aaron.ballman, JonasToth.
Herald added subscribers: carlosgalvezp, xazax.hun.
Herald added a project: All.
njames93 requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.
Adds an option to specify where the `const` should be placed when emitting
fixes.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D131386
Files:
clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp
clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h
clang-tools-extra/docs/clang-tidy/checks/misc/const-correctness.rst
clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-alignment.cpp
Index: clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-alignment.cpp
===================================================================
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-alignment.cpp
@@ -0,0 +1,46 @@
+// RUN: %check_clang_tidy %s misc-const-correctness %t -check-suffixes=",WEST" \
+// RUN: -config="{CheckOptions: { \
+// RUN: misc-const-correctness.ConstAlignment: west, \
+// RUN: misc-const-correctness.WarnPointersAsValues: true, \
+// RUN: misc-const-correctness.TransformPointersAsValues: true \
+// RUN: }}"
+// RUN: %check_clang_tidy %s misc-const-correctness %t -check-suffixes=",WEST" \
+// RUN: -config="{CheckOptions: { \
+// RUN: misc-const-correctness.ConstAlignment: left, \
+// RUN: misc-const-correctness.WarnPointersAsValues: true, \
+// RUN: misc-const-correctness.TransformPointersAsValues: true \
+// RUN: }}"
+// RUN: %check_clang_tidy %s misc-const-correctness %t -check-suffixes=",EAST" \
+// RUN: -config="{CheckOptions: { \
+// RUN: misc-const-correctness.ConstAlignment: east, \
+// RUN: misc-const-correctness.WarnPointersAsValues: true, \
+// RUN: misc-const-correctness.TransformPointersAsValues: true \
+// RUN: }}"
+// RUN: %check_clang_tidy %s misc-const-correctness %t -check-suffixes=",EAST" \
+// RUN: -config="{CheckOptions: { \
+// RUN: misc-const-correctness.ConstAlignment: right, \
+// RUN: misc-const-correctness.WarnPointersAsValues: true, \
+// RUN: misc-const-correctness.TransformPointersAsValues: true \
+// RUN: }}"
+
+void foo() {
+ int I = 0;
+ // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'I' of type 'int' can be declared 'const'
+ // CHECK-FIXES-WEST: const int I = 0;
+ // CHECK-FIXES-EAST: int const I = 0;
+}
+
+void foo(int I) {
+ int & IRef = I;
+ // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'IRef' of type 'int &' can be declared 'const'
+ // CHECK-FIXES-WEST: const int & IRef = I;
+ // Having the double space and the const attached the the `&` is unfortunate,
+ // however it shouldn't matter for users as fixes get clang-formatted.
+ // CHECK-FIXES-EAST: int const& IRef = I;
+
+ // When decorating a pointer with const, the const always goes to the right
+ // of the `*`.
+ int *IPtr = &I;
+ // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'IPtr' of type 'int *' can be declared 'const'
+ // CHECK-FIXES: int *const IPtr = &I;
+}
Index: clang-tools-extra/docs/clang-tidy/checks/misc/const-correctness.rst
===================================================================
--- clang-tools-extra/docs/clang-tidy/checks/misc/const-correctness.rst
+++ clang-tools-extra/docs/clang-tidy/checks/misc/const-correctness.rst
@@ -148,3 +148,25 @@
int *changing_pointee = &value;
changing_pointee = &result;
+.. option:: ConstAlignment
+
+ Controls where to insert ``const`` when creating fix-it hints, defaults to ``right``.
+
+ - ``left``:
+
+ .. code-block:: c++
+
+ const int I = 0;
+ const int &IRef = I;
+ int *const IPtr = &I;
+
+
+ - ``right``:
+
+ .. code-block:: c++
+
+ int const I = 0;
+ int const &IRef = I;
+ int *const IPtr = &I;
+
+ ``east`` and ``west`` aliases are also supported.
Index: clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h
===================================================================
--- clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h
+++ clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h
@@ -10,6 +10,7 @@
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_CONSTCORRECTNESSCHECK_H
#include "../ClangTidyCheck.h"
+#include "../utils/FixItHintUtils.h"
#include "clang/Analysis/Analyses/ExprMutationAnalyzer.h"
#include "llvm/ADT/DenseSet.h"
@@ -48,6 +49,8 @@
const bool TransformValues;
const bool TransformReferences;
const bool TransformPointersAsValues;
+
+ const utils::fixit::QualifierPolicy ConstAlignment;
};
} // namespace misc
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
@@ -18,6 +18,22 @@
namespace clang {
namespace tidy {
+
+template <> struct OptionEnumMapping<utils::fixit::QualifierPolicy> {
+ static ArrayRef<std::pair<utils::fixit::QualifierPolicy, StringRef>>
+ getEnumMapping() {
+ static constexpr std::pair<utils::fixit::QualifierPolicy, StringRef>
+ Mapping[] = {
+ {utils::fixit::QualifierPolicy::Left, "left"},
+ {utils::fixit::QualifierPolicy::Right, "right"},
+ // Let people use east/west spelling.
+ {utils::fixit::QualifierPolicy::Left, "west"},
+ {utils::fixit::QualifierPolicy::Right, "east"},
+ };
+ return Mapping;
+ }
+};
+
namespace misc {
namespace {
@@ -45,7 +61,9 @@
TransformValues(Options.get("TransformValues", true)),
TransformReferences(Options.get("TransformReferences", true)),
TransformPointersAsValues(
- Options.get("TransformPointersAsValues", false)) {
+ Options.get("TransformPointersAsValues", false)),
+ ConstAlignment(
+ Options.get("ConstAlignment", utils::fixit::QualifierPolicy::Right)) {
if (AnalyzeValues == false && AnalyzeReferences == false)
this->configurationDiag(
"The check 'misc-const-correctness' will not "
@@ -61,6 +79,8 @@
Options.store(Opts, "TransformValues", TransformValues);
Options.store(Opts, "TransformReferences", TransformReferences);
Options.store(Opts, "TransformPointersAsValues", TransformPointersAsValues);
+
+ Options.store(Opts, "ConstAlignment", ConstAlignment);
}
void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) {
@@ -173,7 +193,7 @@
if (VC == VariableCategory::Value && TransformValues) {
Diag << addQualifierToVarDecl(*Variable, *Result.Context,
DeclSpec::TQ_const, QualifierTarget::Value,
- QualifierPolicy::Right);
+ ConstAlignment);
// FIXME: Add '{}' for default initialization if no user-defined default
// constructor exists and there is no initializer.
return;
@@ -182,7 +202,7 @@
if (VC == VariableCategory::Reference && TransformReferences) {
Diag << addQualifierToVarDecl(*Variable, *Result.Context,
DeclSpec::TQ_const, QualifierTarget::Value,
- QualifierPolicy::Right);
+ ConstAlignment);
return;
}
@@ -190,7 +210,7 @@
if (WarnPointersAsValues && TransformPointersAsValues) {
Diag << addQualifierToVarDecl(*Variable, *Result.Context,
DeclSpec::TQ_const, QualifierTarget::Value,
- QualifierPolicy::Right);
+ ConstAlignment);
}
return;
}
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits