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

Dmitri Gribenko via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 27 02:43:57 PST 2020


gribozavr2 added a comment.

Thank you for the contribution! I didn't review the code thoroughly yet, only the tests.



================
Comment at: clang-tools-extra/clang-tidy/misc/MissingHeaderFileDeclarationCheck.cpp:29
+
+  bool isHeader() const {
+    return llvm::StringSwitch<bool>(Extension)
----------------
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.


================
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
----------------
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.


================
Comment at: clang-tools-extra/docs/clang-tidy/checks/misc-missing-header-file-declaration.rst:14
+   
+   If set to `1` the check will look in any header file for a matching
+   declaration.
----------------
Unclear what happens when CheckAnyHeader=0.


================
Comment at: clang-tools-extra/test/clang-tidy/checkers/Inputs/misc-missing-header-file-declaration/misc-missing-header-file-declaration.cpp.tmp.h:22
+extern void nS();
+} // namespace ns2
+
----------------
Need a test for a template defined in a header (shouldn't produce a warning).

Need a test for an inline function defined in a header (also no warning).

Need a test for a static function defined in a header (should produce no warning, but it is bad for other reasons, however I think they are out of scope for this checker).

Need a test for an anonymous namespace in a header (no warning).


================
Comment at: clang-tools-extra/test/clang-tidy/checkers/misc-missing-header-file-declaration.cpp:1
+// RUN: %check_clang_tidy %s misc-missing-header-file-declaration %t -- \
+// RUN: -- -I%S/Inputs/misc-missing-header-file-declaration
----------------
Need lots of tests for classes, out-of-line member declarations, member templates, out-of-line member templates, class templates, variable templates etc.


================
Comment at: clang-tools-extra/test/clang-tidy/checkers/misc-missing-header-file-declaration.cpp:80
+void templateFuncNoHeader() {}
+// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: Function 'templateFuncNoHeader' is defined with external linkage
+
----------------
Need a test for a template in an anonymous namespace.


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