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

Viktoriia Bakalova via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 9 04:42:26 PST 2023


VitaNuo 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))
----------------
hokein wrote:
> 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*`.
Oh right, I didn't notice. Thanks.


================
Comment at: clang-tools-extra/include-cleaner/lib/Record.cpp:387
+  private:
+  bool isImplicitTemplateInstantiation(const NamedDecl *D) {   
+    return isImplicitTemplateSpecialization<FunctionDecl>(D) ||
----------------
hokein wrote:
> 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`.
I am not sure I fully understand this comment, since I cannot both move this method to the anonymous namespace above and inline it at the same time. I have inlined it now, hopefully it looks fine to you.


================
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(
----------------
hokein wrote:
> maybe DropImplicitTemplateTopDecl?
Not sure, since the rest of tests above only use nouns, e.g., "Macros", "Inclusion", "Namespace", etc. I've called it "ImplicitTemplates" now, hopefully it's fine.


================
Comment at: clang-tools-extra/include-cleaner/unittests/RecordTest.cpp:120
+  };
+  int v = dispatch<MyGetter>();
+  )cpp";
----------------
hokein wrote:
> 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);
> ```
You're right, but IMHO I would rather keep the current example. The current example, although simplified, still highly resembles the pattern we've found it real code. The snippet above does not anymore. Since it's a bug fix for a real pattern, I'd rather keep it as is. Let me know if you disagree.


================
Comment at: clang-tools-extra/include-cleaner/unittests/RecordTest.cpp:123
+  auto AST = build();
+  EXPECT_THAT(Recorded.Roots, testing::Not(testing::ElementsAre(named("get"))));
+}
----------------
hokein wrote:
> 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.
Sure, I have no preference.


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