This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Abseil: no namespace check
ClosedPublic

Authored by deannagarcia on Aug 10 2018, 1:22 PM.

Details

Summary

This check ensures that users of Abseil do not open namespace absl in their code, as that violates our compatibility guidelines.

AbseilMatcher.h written by Hugo Gonzalez.

Diff Detail

Event Timeline

deannagarcia created this revision.Aug 10 2018, 1:22 PM

Check documentation is missing.

clang-tidy/abseil/NoNamespaceCheck.cpp
22

Please place return in separate line.

clang-tidy/abseil/NoNamespaceCheck.h
26

Please run Clang-format.

docs/ReleaseNotes.rst
60–83

Please remove placeholder.

88

Please reformulate to refer to code, not user. Please enclose absl in ``.

89

abseil ->Abseil.

Could it happen that some template specializations or so need to land in absl?

clang-tidy/abseil/NoNamespaceCheck.cpp
29

Please follow LLVM naming convention (Capitalize, and Decl should crash with a type)

aaron.ballman added inline comments.Aug 12 2018, 7:08 AM
clang-tidy/abseil/NoNamespaceCheck.cpp
24

I think this needs a not(isExpansionInSystemHeader()) in there, as this is going to trigger on code that includes a header file using an absl namespace. If I'm incorrect and users typically include abseil as something other than system includes, you'll have to find a different way to solve this.

33

in the user code -> in user code

Does Abseil prohibit the user from specializing templates in the absl namespace with user-defined types?

We are aware that this test will cause warnings on users code through their dependencies on abseil.
However, from what we know it seems like these warnings are normally suppressed.
If anyone has a good idea on how to avoid this/has insight on whether this will be a problem for the average user, please let me know!

Suppressed how?
I think this needs tests.

The check is missing its document, please add one in docs/clang-tidy/checks/.

clang-tidy/abseil/NoNamespaceCheck.cpp
24

Yeah, we have discussed this issue internally. abseil-user code usually includes abseil header like #include "whatever/abseil/base/optimization.h", so IsExpansionInSystemHeader doesn't work for most cases.

We need a way to filter out all headers being a part of abseil library. I think we can create a matcher InExpansionInAbseilHeader -- basically using IsExpansionInFileMatching with a regex expression that matches typical abseil include path. What do you think?

And we'll have more abseil checks (e.g. abseil-no-internal-deps) that use InExpansionInAbseilHeader, we should put it to a common header.

aaron.ballman added inline comments.Aug 13 2018, 4:53 AM
clang-tidy/abseil/NoNamespaceCheck.cpp
24

I think that is a sensible approach.

deannagarcia marked 7 inline comments as done.
deannagarcia added inline comments.
clang-tidy/abseil/NoNamespaceCheck.cpp
24

We will begin working on this, I think it will first be implemented in abseil-no-internal-deps but once it looks good I will add it to this patch.

hugoeg added a subscriber: hugoeg.Aug 13 2018, 11:39 AM
hugoeg added inline comments.
clang-tidy/abseil/NoNamespaceCheck.cpp
24

I'm going to go ahead a implement this in ASTMatchers.h and include it on the patch for abseil-no-internal-deps

JonasToth added inline comments.Aug 13 2018, 1:01 PM
clang-tidy/abseil/NoNamespaceCheck.cpp
24

In principle it is ok, but I don't think ASTMatchers.h is the correct place, because it is only of use to clang-tidy.

There is a util directory in clang-tidy for this kind of stuff. Defining you own matchers works the same as in ASTMatchers, you can grep through clang-tidy checks (e.g. AST_MATCHER) to have some examples of private matchers.

hokein added inline comments.Aug 16 2018, 5:28 AM
clang-tidy/abseil/NoNamespaceCheck.cpp
24

ASTMatchers.h is not a reasonable place to put asseil-specific matchers. We have clang-tidy/utils/Matchers.h for putting clang-tidy specific matchers. I'm not sure whether it is reasonable to put abseil-specific matchers there. Maybe we create a AbseilMatcher.h file in clang-tidy/abseil directory?

That sounds good as well, just not in clang and best in clang-tidy/utils

ASTMatchers.h is not a reasonable place to put asseil-specific matchers. We have clang-tidy/utils/Matchers.h for putting clang-tidy specific matchers. I'm not sure whether it is reasonable to put abseil-specific matchers there. Maybe we create a AbseilMatcher.h file in clang-tidy/abseil directory?

https://reviews.llvm.org/D50580

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

This revision includes a matcher so that the warning does not trigger on internal Abseil files.

clang-tidy/abseil/NoNamespaceCheck.cpp
24

I put it in clang-tidy/abseil for now but I can definitely move it to clang-tidy/utils/ if you would prefer that

hokein added inline comments.Aug 21 2018, 1:14 AM
clang-tidy/abseil/AbseilMatcher.h
17

nit: We need proper documentation for this matcher, since it is exposed to users.

18
  • using negative for AST matcher seems uncommon in ASTMatchers, for negative we have unless, so I'd suggest implementing isInAbseilHeader.
  • it seems that this matcher is very similar to the matcher in https://reviews.llvm.org/D50542. I think isInAbseilFile should also cover your case here, and also ignore the warning if you run the check on abseil core files.
33

The regex seems incomplete, we are missing algorithm, time subdirectory.

34

typo: utility

35

nit: remove the outer ().

lebedev.ri added inline comments.Aug 21 2018, 1:20 AM
clang-tidy/abseil/AbseilMatcher.h
33

So what happens if open the namespace in a file that is located in my personal absl/base directory?
It will be false-negatively not reported?

hokein added inline comments.Aug 21 2018, 1:32 AM
clang-tidy/abseil/AbseilMatcher.h
33

Yes, I'd say this is a known limitation.

lebedev.ri added inline comments.Aug 21 2018, 1:36 AM
clang-tidy/abseil/AbseilMatcher.h
33

Similarly, the check will break (start producing false-positives) as soon as a new directory is added to abseil proper.
I don't have any reliable idea on how to do this better, but the current solution seems rather suboptimal..

deannagarcia marked 12 inline comments as done.
deannagarcia edited the summary of this revision. (Show Details)Aug 21 2018, 7:53 AM
deannagarcia added inline comments.
clang-tidy/abseil/AbseilMatcher.h
33

We are aware that there will be a false-negative if code opens namespace in the absl directories, however we think that is pretty rare and that users shouldn't be doing that anyway. No matter how we do this there will be a way for users to circumvent the check, and we will allow those users to do it because it will be their code that breaks in the end.

33

The false-positive with new directories is something we thought about, but new directories aren't added to absl very often so we thought it wouldn't be too hard to add them to this regex when they are.

hugoeg added inline comments.Aug 21 2018, 8:05 AM
clang-tidy/abseil/AbseilMatcher.h
33

Members of the Abseil team can also update this check as new directories are release

Thanks for the updates. Looks mostly good, a few nits.

clang-tidy/abseil/AbseilMatcher.h
33

nit: this matcher name now should be isInAbseilFile.

clang-tidy/abseil/NoNamespaceCheck.cpp
27

Does the absl namespace is always the top level namespace? If so, I think it would be safer to use qualified name "::absl" here.

clang-tidy/abseil/NoNamespaceCheck.h
20

nit: test => check.

21

nit: our = abseil?

test/clang-tidy/abseil-no-namespace.cpp
23

nit: put it immediately after the above name absl {.

Please rebase from trunk.

docs/ReleaseNotes.rst
63

Just Ensures. Please use `` for language constructs.

docs/clang-tidy/checks/abseil-no-namespace.rst
7

Please synchronize first sentence with Release Notes.

8

Abseil

11

Code instead of user, please.

21

Please remove space after https:

deannagarcia marked 11 inline comments as done.
Eugene.Zelenko added inline comments.Aug 21 2018, 2:55 PM
docs/ReleaseNotes.rst
60

Please sort new checks list alphabetically.

85

Please remove merge conflict residual.

deannagarcia marked 2 inline comments as done.
deannagarcia marked 6 inline comments as done.Aug 22 2018, 7:20 AM
deannagarcia edited the summary of this revision. (Show Details)Aug 22 2018, 7:32 AM
hokein accepted this revision.Aug 23 2018, 1:18 AM

Looks good.

test/clang-tidy/abseil-no-namespace.cpp
11

Does the test get passed on the first command %check_clang_tidy %s abseil-no-namespace %t -- -- -I %S/Inputs? The first command will suppress all warning in headers, I think it will fail?

This revision is now accepted and ready to land.Aug 23 2018, 1:18 AM

Rebased the patch

deannagarcia added inline comments.Aug 24 2018, 10:44 AM
test/clang-tidy/abseil-no-namespace.cpp
11

The test passes, and I'm pretty sure it's because this is a CHECK not a CHECK-MESSAGES

hokein closed this revision.Aug 28 2018, 12:50 AM

I have committed the patch for you, rL340800.

test/clang-tidy/abseil-no-namespace.cpp
11

Ah, I thought it was CHECK-MESSAGES, note that CHECK and CHECK-MESSAGE don't match multiple lines, the message should be one-line. I have fixed for you.