[PATCH] D141271: [include-cleaner] Filter template instantiations from AST roots.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 9 04:11:32 PST 2023


hokein added inline comments.


================
Comment at: clang-tools-extra/include-cleaner/lib/Record.cpp:377
           continue;
-        // FIXME: Filter out certain Obj-C and template-related decls.
+        if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
+          if (isImplicitTemplateInstantiation(ND))
----------------
as we already do a dyn_cast inside the `isImplicitTemplateInstantiation`, we can get rid of this NamedDecl cast by changing the `isImplicitTemplateInstantiation` to accept a `Decl*`.


================
Comment at: clang-tools-extra/include-cleaner/lib/Record.cpp:387
+  private:
+  bool isImplicitTemplateInstantiation(const NamedDecl *D) {   
+    return isImplicitTemplateSpecialization<FunctionDecl>(D) ||
----------------
we can move it to the above anonymous namespace as well (it is fine to have two functions with same name because of C++ overloads). In this case, I'd probably inline it in `HandleTopLevelDecl`.


================
Comment at: clang-tools-extra/include-cleaner/unittests/RecordTest.cpp:105
+// Decl from template instantiation is filtered out from roots.
+TEST_F(RecordASTTest, TemplateInstantiations) {
+  Inputs.ExtraFiles["dispatch.h"] = R"cpp(
----------------
maybe DropImplicitTemplateTopDecl?


================
Comment at: clang-tools-extra/include-cleaner/unittests/RecordTest.cpp:120
+  };
+  int v = dispatch<MyGetter>();
+  )cpp";
----------------
this example is a nice example to show the original bug (where we inserted unexpected additional headers) in include-cleaner. 

The scope of this patch is to filter out the implicit template instantiation, a simple testcase like below could work. (the current testcase is fine as well, up to you)

```
template<typename T>
int func(T) { return 0; } 
auto s = func<int>(1);
```


================
Comment at: clang-tools-extra/include-cleaner/unittests/RecordTest.cpp:123
+  auto AST = build();
+  EXPECT_THAT(Recorded.Roots, testing::Not(testing::ElementsAre(named("get"))));
+}
----------------
nit: instead doing a negative verification, I'd check the captured root decl, which is (a `MyGetter` CXXRecordDecl, and a `v` var decl), and with a comment saying the implicitly-instantiated method decl is filtered out.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D141271



More information about the cfe-commits mailing list