This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Adding Fuchsia checkers to clang-tidy
ClosedPublic

Authored by juliehockett on Nov 15 2017, 4:20 PM.

Details

Summary

Adds a new Fuchsia module to clang-tidy to warn for features that are
disallowed in the Fuchsia and Zircon style guides.

Fuchsia is a modular, capability-based operating system. Fuchsia and its
Zircon kernel use a subset of the C++14 language, and so these checks
are designed to prompt the user to avoid disallowed behaviors.

Adds a check to the Fuchsia modules to warn when functions are declared
or called with default arguments.

See https://fuchsia.googlesource.com/zircon/+/master/docs/cxx.md for
reference.

Diff Detail

Event Timeline

juliehockett created this revision.Nov 15 2017, 4:20 PM
Eugene.Zelenko set the repository for this revision to rL LLVM.

Please split this review with one check per review.

docs/ReleaseNotes.rst
83

Please fix double space

docs/clang-tidy/checks/fuchsia-default-arguments.rst
8

Please don't use abbreviations. Same in other places.

19

Please insert empty line above.

docs/clang-tidy/checks/fuchsia-statically-constructed-objects.rst
7

Please highlight constexpr with ``. Same in other places.

docs/clang-tidy/checks/fuchsia-thread-local.rst
6

Please highlight thread-local and extern with ``. Same in other places.

Eugene.Zelenko added inline comments.Nov 15 2017, 6:39 PM
clang-tidy/fuchsia/FuchsiaTidyModule.cpp
31

Please remove empty line.

clang-tidy/fuchsia/MultipleInheritanceCheck.cpp
100

Please remove empty line.

docs/ReleaseNotes.rst
63

Check to should be removed, just Prevent. Same in other places.

Please mention new module in docs/clang-tidy/index.rst.

For background: what is Fuchsia and where do these requirements come from (are they documented publicly somewhere)?

We tend to prefer concise patches over code dumps, so I think it would make sense to split this review into multiple patches. The first one can be adding the module and a single check along with background information on why the module would be appropriate. The rest of the patches should be one check per patch.

juliehockett marked an inline comment as done.
juliehockett edited the summary of this revision. (Show Details)
juliehockett edited the summary of this revision. (Show Details)
juliehockett marked 5 inline comments as done.Nov 16 2017, 12:29 PM
docs/ReleaseNotes.rst
60

Check name and link was accidentally removed.

docs/clang-tidy/checks/list.rst
58

List should be pruned.

juliehockett edited the summary of this revision. (Show Details)

Updated docs

juliehockett marked 2 inline comments as done.Nov 16 2017, 5:40 PM

We do need to figure out what the right prefix for these checks is (whether fuchsia-* since they'll be used under the Fuchsia umbrella, zircon-* since the follow the Zircon section of the style guide linked above, or the more generic google-*).

Does Fuchsia coding conventions allows default parameters in class methods? If not, test should reflect this

docs/ReleaseNotes.rst
63

I think will be good idea to have description same as first sentence in documentation, i. e. Warns if a function is declared or called with default arguments.

test/clang-tidy/fuchsia-default-arguments.cpp
7

Please remove void (see modernize-redundant-void-arg). Same below.

We do need to figure out what the right prefix for these checks is (whether fuchsia-* since they'll be used under the Fuchsia umbrella, zircon-* since the follow the Zircon section of the style guide linked above, or the more generic google-*).

I think it should use project-specific prefix, since it's open source project. Google may have different coding guidelines for other projects.

juliehockett marked 2 inline comments as done.

Updated docs and added tests to check class methods.

I think it should use project-specific prefix, since it's open source project. Google may have different coding guidelines for other projects.

Reasonable. It makes sense to consider it on a check-by-check basis too, I'd think.

Eugene.Zelenko added inline comments.Nov 17 2017, 5:08 PM
docs/ReleaseNotes.rst
63

I think will be good idea to change to function to function or method. Same in documentation.

docs/clang-tidy/checks/fuchsia-default-arguments.rst
9

I briefly look on other checks documentation, so will be good idea to use just Example: or For example, the declaration: . But will be good idea to hear opinion of native English speaker.

aaron.ballman requested changes to this revision.Nov 18 2017, 10:48 AM

Thank you for the explanation about what is driving this and the documentation. I don't think google-* is the correct module for this as it's too general of an umbrella. I have a slight preference for zircon-* because this appears to be specific to that particular project.

clang-tidy/fuchsia/DefaultArgumentsCheck.cpp
19

I think this should be put into ASTMatchers.h as a separate patch (feel free to list me as a reviewer). See D39940 for an example of how to do that, but the only non-obvious task is running clang\docs\tools\dump_ast_matchers.py to generate the HTML from ASTMatchers.h.

23

Isn't this case already covered by the other matcher? Or do you have system header files which have default arguments and those functions are disallowed by the style guide, but cannot be removed from the system header?

29

You can use const auto * here instead of spelling the type out twice.

37

Would it make sense to add a fix-it that removes the intializer for the default argument?

docs/clang-tidy/checks/fuchsia-default-arguments.rst
9

I'd go with: For example, the declaration:

docs/clang-tidy/index.rst
672 ↗(On Diff #123446)

Spurious newline removal?

This revision now requires changes to proceed.Nov 18 2017, 10:48 AM
juliehockett edited edge metadata.
juliehockett marked 8 inline comments as done.

Moved AST matcher out to ASTMatchers.h (see D40261), fixing comments

clang-tidy/fuchsia/DefaultArgumentsCheck.cpp
23

The latter -- there's a bunch of third-party code and the like that could have default arguments, but shouldn't be used like that in the project.

Did you decide on fuchsia instead of zircon for the module name, or is that decision still up in the air?

clang-tidy/fuchsia/DefaultArgumentsCheck.cpp
23

Ah, that makes sense. Thanks!

30

I think the diagnostic should be in singular form. How about: calling a function that uses a default argument is disallowed?

36–39

Does getDefaultArgRange() not provide the correct range already (so you don't need to go back to the original source)? Or does that range miss the =?

You might want to disable the fix-it in the case getDefaultArgRange() returns an empty range, or in case the default arg expression comes from a macro (and add a test for the latter case). e.g.,

#define DERP(val)  = val

void f(int i DERP);

Additionally, a test where the identifier is elided would be good as well: void f(int = 12);

42

This diagnostic could be similarly tightened -- the function isn't the issue, the parameter with the default argument is. Perhaps: declaring a parameter with a default value is disallowed?

docs/clang-tidy/checks/fuchsia-default-arguments.rst
17–18

The wording here is a bit confusing; it's not that calling it with no arguments causes the warning, it's that calling it such that the default argument value is used that's the problem. Perhaps: A function call expression that uses a default argument will be diagnosed. or something along those lines.

juliehockett marked 6 inline comments as done.

Disabled the fix-it for getDefaultArgRange() that returns an empty range and for default args that come from a macro.

We're going to go with fuchsia-* for the module name, since the style applies to the broader project. Also, there may be zircon-specific checks at some point, so we want to leave the door open for that.

clang-tidy/fuchsia/DefaultArgumentsCheck.cpp
36–39

getDefaultArgRange() misses the = -- though if there's a better way to do it I'm all ears!

aaron.ballman added inline comments.Nov 23 2017, 8:16 AM
clang-tidy/fuchsia/DefaultArgumentsCheck.cpp
32

Drop "the" from the diagnostic to match wording in other places

36–39

In that case, this seems fine to me.

45

Drop "the" here as well.

49–53

This can be hoisted to the initialization: SourceLocation StartLocation = D->getName().empty() ? D->getLocStart() : D->getLocation();

test/clang-tidy/fuchsia-default-arguments.cpp
41

I think this diagnostic should be suppressed -- having it on the declaration where the default argument is defined should be sufficient, no?

58

Two more test cases that should be added:

void f(int i);
void f(int i = 12);

void f(int i) {
    
}

struct S {
  void f(int i);
};

void S::f(int i = 12) {}

int main() {
  S s;
  s.f();
  f();
}
juliehockett marked 7 inline comments as done.

Added new tests and updated wording in warning.

docs/clang-tidy/checks/fuchsia-default-arguments.rst
7

Please synchronize with text in Release Notes.

juliehockett marked an inline comment as done.

Updating docs

aaron.ballman accepted this revision.Nov 28 2017, 6:01 AM

LGTM aside from one small nit with the test file missing a trailing newline.

test/clang-tidy/fuchsia-default-arguments.cpp
20

Please add a newline at the end of the file.

This revision is now accepted and ready to land.Nov 28 2017, 6:01 AM
juliehockett marked an inline comment as done.

Added missing newline

Thanks for the nit fix. If you'd like me to commit this on your behalf, just let me know.

If you could, that'd be great! Thank you!

aaron.ballman closed this revision.Nov 28 2017, 1:10 PM

I've commit in r319225.

If you could, that'd be great! Thank you!

It'll be good idea it you'll request commit rights, since you have more checks in development queue.

alexfh edited edge metadata.Nov 29 2017, 6:40 AM

A couple of late comments.

clang-tidy/fuchsia/DefaultArgumentsCheck.cpp
40
57

This argument is not used in the message. Does it have any effect at all?

docs/clang-tidy/index.rst
64 ↗(On Diff #124586)

How about adding a link to the coding style document? (We should do this for other entries here, but that's unrelated to your patch.)