This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Added option for disabling const qualifiers in readability-qualified-auto
ClosedPublic

Authored by njames93 on Jan 28 2020, 6:23 AM.

Details

Summary

Adds an option called AddConstToQualified to readability-qualified-auto to toggle adding const to the auto typed pointers and references. By default its enabled but in the LLVM module its disabled.

Diff Detail

Event Timeline

njames93 created this revision.Jan 28 2020, 6:23 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 28 2020, 6:23 AM
njames93 added a project: Restricted Project.

SGTM, but please wait for the reviewers.

njames93 marked an inline comment as done.Jan 28 2020, 6:37 AM
njames93 added inline comments.
clang-tools-extra/clang-tidy/readability/QualifiedAutoCheck.cpp
209

If a variables is typed as auto x = cast<const int*>(y), should the behaviour be const auto*x = ... even if AddConstQualifier is turned off. Thereby making the AddConstQualifier check only there to ensure that variables already typed as auto * and auto & get const qualifier if necessary

Unit tests: pass. 62260 tests passed, 0 failed and 827 were skipped.

clang-tidy: pass.

clang-format: pass.

Build artifacts: diff.json, clang-tidy.txt, clang-format.patch, CMakeCache.txt, console-log.txt, test-results.xml

Pre-merge checks is in beta. Report issue. Please join beta or enable it for your project.

Quuxplusone added inline comments.
clang-tools-extra/docs/clang-tidy/checks/readability-qualified-auto.rst
41

Is it worth adding an example of a double pointer?

auto BarN = cast<int **>(FooN);

Does that become auto* or auto** (and why)? My wild guess is that it becomes auto* (and because nobody cares about double pointers), but I could be wrong.

64

s/Will be/will be/
s/will get be/will be/

In the preceding section you give an example with volatile. How about here? What happens with

auto *volatile Foo3 = cast<const int *>(Bar3);
auto *Foo4 = cast<volatile int *>(Bar4);

Do they become

const auto *volatile Foo3 = cast<const int *>(Bar3);
volatile auto *Foo4 = cast<volatile int *>(Bar4);

as I would expect?

69

How does this option interact with

const int range[10];
for (auto&& elt : range)

Does it (incorrectly IMHO) make that const auto&& elt, or (correctly IMHO) const auto& elt, or (also correctly) leave it as [auto&&, which Always Works](https://quuxplusone.github.io/blog/2018/12/15/autorefref-always-works/)?

clang-tools-extra/test/clang-tidy/checkers/llvm-qualified-auto.cpp
20

It is worth adding one test case to show that the LLVM alias does not gratuitously remove const from declarations:

auto const &ExtendedConstPtr = getCIntPtr();
// becomes
auto *const &ExtendedConstPtr = getCIntPtr();
njames93 marked 7 inline comments as done.Jan 28 2020, 8:35 AM
njames93 added inline comments.
clang-tools-extra/docs/clang-tidy/checks/readability-qualified-auto.rst
41

Double pointers are just resolved to auto *.

64

Good spot on the Will/be/get/(whatever I tried to type)

the check keeps local volatile qualifiers, but it wont add volatile pointer (or ref) qualifiers even if the deduced type is a volatile pointer. In the first patch we came to the conclusion not to add them.

69

RValueReferences are ignored

clang-tools-extra/test/clang-tidy/checkers/llvm-qualified-auto.cpp
20

That test case looks malformed as the function getCIntPtr() doesn't return a reference. as for removing const etc those cases are all in the readability-qualified-auto.cpp test file

aaron.ballman added inline comments.Jan 28 2020, 1:41 PM
clang-tools-extra/docs/clang-tidy/checks/readability-qualified-auto.rst
41

They resolve to auto * today but I could see a real argument that they should resolve to auto ** instead based on the same logic of: don't make the reader infer pointer/references and qualifiers.

njames93 marked 4 inline comments as done.Jan 29 2020, 2:36 AM
njames93 added inline comments.
clang-tools-extra/docs/clang-tidy/checks/readability-qualified-auto.rst
41

One change per patch...

njames93 updated this revision to Diff 241082.Jan 29 2020, 2:47 AM
  • Always add const to auto typed variable.

This update adds const to variables just typed with auto, but wont enforce checking on auto * or auto & unless AddConstToQualified is set.

njames93 edited the summary of this revision. (Show Details)Jan 29 2020, 2:47 AM

Unit tests: fail. 62275 tests passed, 1 failed and 827 were skipped.

failed: Clang.CodeGenOpenCL/amdgpu-features.cl

clang-tidy: pass.

clang-format: pass.

Build artifacts: diff.json, clang-tidy.txt, clang-format.patch, CMakeCache.txt, console-log.txt, test-results.xml

Pre-merge checks is in beta. Report issue. Please join beta or enable it for your project.

aaron.ballman added inline comments.Jan 29 2020, 5:00 AM
clang-tools-extra/docs/clang-tidy/checks/readability-qualified-auto.rst
41

I wasn't suggesting a change to this patch, I was checking to see if there's sentiment that we missed this case in the first place. Sorry for not making that more clear.

njames93 updated this revision to Diff 241374.Jan 30 2020, 2:13 AM
  • Streamline fixits
  • Add documentation about double pointers, maybe a follow up patch to fix

Unit tests: fail. 62275 tests passed, 1 failed and 827 were skipped.

failed: Clang.CodeGenOpenCL/amdgpu-features.cl

clang-tidy: pass.

clang-format: fail. Please format your changes with clang-format by running git-clang-format HEAD^ or applying this patch.

Build artifacts: diff.json, clang-tidy.txt, clang-format.patch, CMakeCache.txt, console-log.txt, test-results.xml

Pre-merge checks is in beta. Report issue. Please join beta or enable it for your project.

njames93 updated this revision to Diff 241924.Feb 2 2020, 7:33 AM
  • Small reformat

Unit tests: pass. 62383 tests passed, 0 failed and 839 were skipped.

clang-tidy: pass.

clang-format: pass.

Build artifacts: diff.json, clang-tidy.txt, clang-format.patch, CMakeCache.txt, console-log.txt, test-results.xml

Pre-merge checks is in beta. Report issue. Please join beta or enable it for your project.

aaron.ballman accepted this revision.Feb 2 2020, 10:14 AM

LGTM!

clang-tools-extra/clang-tidy/readability/QualifiedAutoCheck.cpp
276

Not specific to this patch, so feel free to address in another one -- I think you should be passing Var instead of Var->getName() -- the diagnostics engine does magic to format the name properly for any NamedDecl, including proper quoting. (Similar applies elsewhere.)

This revision is now accepted and ready to land.Feb 2 2020, 10:14 AM
njames93 marked 2 inline comments as done.Feb 2 2020, 1:24 PM
njames93 added inline comments.
clang-tools-extra/clang-tidy/readability/QualifiedAutoCheck.cpp
276

I couldn't do that as it actually messes the quotes up: 'auto &'TdNakedCRef''

This revision was automatically updated to reflect the committed changes.
njames93 marked an inline comment as done.
aaron.ballman added inline comments.Feb 2 2020, 1:35 PM
clang-tools-extra/clang-tidy/readability/QualifiedAutoCheck.cpp
276

Ah, good point. I was thinking about variable template specializations, but I'm not certain there's an actual issue with them -- it seems like we really want just the base identifier here.