[PATCH] D73413: [clang-tidy] Add check to detect external definitions with no header declaration

Nathan James via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 27 05:08:29 PST 2020


njames93 marked 2 inline comments as done.
njames93 added inline comments.


================
Comment at: clang-tools-extra/clang-tidy/misc/MissingHeaderFileDeclarationCheck.cpp:29
+
+  bool isHeader() const {
+    return llvm::StringSwitch<bool>(Extension)
----------------
gribozavr2 wrote:
> njames93 wrote:
> > gribozavr2 wrote:
> > > I think we should consider any file other than the main file to be a header (anything brought in by `#include`). Projects have lots of different convention -- for example, LLVM uses a `.inc` extension for some generated files and x-macro files. The standard library does not have any extensions at all (and I'm sure some projects imitate that) etc.
> > I agree in part, but there is one case that I didn't want to happen and that is when clang-tidy is ran as a text editor plugin. Often it will blindly treat the current open file as the main file which would lead to a lot of false positives, Would a better metric being matching on file extensions not corresponding to source files (c, cc, cxx, cpp)?
> > I agree in part, but there is one case that I didn't want to happen and that is when clang-tidy is ran as a text editor plugin.
> 
> Does that really happen? That sounds like broken editor integration to me, because this is not how C++ without modules works...
> 
> > Would a better metric being matching on file extensions not corresponding to source files (c, cc, cxx, cpp)?
> 
> Yes, I think that would be better, if we have to sniff file extensions.
Well it depends on the editor and language server, I know that if you have a valid compile commands clangd will look for the best source file to compile against. But I feel that there would be some basic editors which will blindly run clang-tidy against the open file. There is also the case of header only libraries that people want to get code analysis on, not sure if they maybe create a proxy file to include the header though.


================
Comment at: clang-tools-extra/clang-tidy/misc/MissingHeaderFileDeclarationCheck.cpp:75
+      continue;
+    if (AnyHeader || File->NamePart.equals_lower(ThisFile->NamePart))
+      return; // Found a good candidate for matching decl
----------------
gribozavr2 wrote:
> njames93 wrote:
> > gribozavr2 wrote:
> > > This heuristic should be a lot more complex. In practice people have more complex naming conventions (for example, in Clang, Sema.h corresponds to many files named SemaSomethingSomething.cpp).
> > > 
> > > I think there is a high chance that checking only a header with a matching name will satisfy too few projects to be worth implementing.
> > > 
> > > On the other hand, if you could use C++ or Clang modules to narrow down the list of headers where the declaration should appear, that would be a much stronger signal.
> > That is the reason I added the CheckAnyHeader option. For small projects the matching name would be usually be enough, but for big complex projects there is no feasible check that would work. Falling back to making sure every external definition has a declaration in at least one header will always be a good warning
> That's the thing -- due to the lack of consistency in projects and style guides for C++, I think most projects will have to turn on CheckAnyHeader. We get implementation complexity in ClangTidy, false positives in high-profile projects, force users to configure ClangTidy to work well for their projects (unless we set CheckAnyHeader=1 by default... which then nobody would flip back to zero), and little benefit for end users.
Would you say the best way would be check if the source file name starts with the header file name by default. Or is that very specific to Clang?

```
/// <SomeHeaderImpl.cpp>
#include "SomeHeader.h"
```
This file would check for declarations in `SomeHeader.h`

Or maybe check if the header file name starts with the source file?

```
/// <SomeHeader.cpp>
#include "SomeHeaderImpl.h"
```
This file would check for declarations in `SomeHeaderImpl.h`.
Or even check both?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D73413/new/

https://reviews.llvm.org/D73413





More information about the cfe-commits mailing list