This is an archive of the discontinued LLVM Phabricator instance.

[ASTImporter] Support TypeTraitExpr Importing
ClosedPublic

Authored by tk1012 on Nov 7 2017, 2:24 AM.

Details

Summary

This fixes unsupporting of importing TypeTraitExpr in ASTImporter.
TypeTraitExpr is caused by "__builtin_types_compatible_p()" that is usually used in the assertion in C code.
For example, PostgreSQL uses the builtin function in its assertion, and ASTImporter currently fails to import the assertion because of diag::err_unsupported_ast_node.

Diff Detail

Repository
rL LLVM

Event Timeline

tk1012 created this revision.Nov 7 2017, 2:24 AM
tk1012 retitled this revision from [ASTImporter] Support Import TypeTraitExpr to [ASTImporter] Support TypeTraitExpr Importing.Nov 13 2017, 2:47 AM
a.sidorin edited edge metadata.Nov 13 2017, 4:31 AM

Hello Takafumi,

Thank you for this patch! I feel positive about it. You can find my comments inline.

lib/AST/ASTImporter.cpp
5540 ↗(On Diff #121853)

We should fail (return nullptr) if import fails and ToTI is nullptr.

5543 ↗(On Diff #121853)

No need to create an ArrayRef - SmallVector can be implicitly converted to it so you can just pass SmallVector to the function.

5545 ↗(On Diff #121853)

The style looks a bit broken. Could you clang-format?

5551 ↗(On Diff #121853)

This will assert if E is value-dependent. You need to check it explicitly.

tk1012 updated this revision to Diff 123539.Nov 19 2017, 11:48 PM

Hello, Aleksei.

I'm sorry for the long response time.
I update the diff to follow your comments.

Updates

  1. I apply clang-format -style=llvm to ASTMatchers.h and ASTImporter.cpp. I don't apply it to ASTImporterTest.cpp for readability of the matching types.
  2. I add return nullptr; if ToTI is nullptr.
  3. I directly pass ToArgVec to TypeTraitExpr::Create()
  4. I add a check for the value-dependent of `E'.
tk1012 marked 3 inline comments as done.Nov 19 2017, 11:49 PM
tk1012 marked an inline comment as done.Nov 19 2017, 11:49 PM

Description of the Sema::BuildTypeTrait()

lib/AST/ASTImporter.cpp
5631 ↗(On Diff #123539)

According to Sema::BuildTypeTrait() in lib/Sema/SemaExprCXX.cpp,
if an argument of TypeTraitExpr has a dependent type,
TypeTraitExpr's value (Result) is always false.

ExprResult Sema::BuildTypeTrait(TypeTrait Kind, SourceLocation KWLoc,
....
 bool Dependent = false;
 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
   if (Args[I]->getType()->isDependentType()) {
     Dependent = true;
     break;
   }
 }

 bool Result = false;
 if (!Dependent)
   Result = evaluateTypeTrait(*this, Kind, KWLoc, Args, RParenLoc);

 return TypeTraitExpr::Create(Context, ResultType, KWLoc, Kind, Args,
                              RParenLoc, Result);
...
a.sidorin accepted this revision.Nov 20 2017, 1:24 AM

LGTM, thank you!

This revision is now accepted and ready to land.Nov 20 2017, 1:24 AM

Hello Aaron,

This patch is OK for me but could you please take a look at ASTMatchers changes?

xazax.hun requested changes to this revision.Nov 21 2017, 6:42 AM

The checker documentation should be updated as well.

include/clang/ASTMatchers/ASTMatchers.h
2251 ↗(On Diff #123539)

Note that the matchers are no longer defined in the header. They are just declared and the definition is moved to a cpp file.

This revision now requires changes to proceed.Nov 21 2017, 6:42 AM
tk1012 updated this revision to Diff 123792.Nov 21 2017, 7:23 AM
tk1012 edited edge metadata.

Hello Gábor,

Thank you for responding.

I move the definition of the mather typeTraitExpr into unittests/AST/ASTImporterTest.cpp.
I also slightly modify the test code.

tk1012 marked an inline comment as done.Nov 21 2017, 7:35 AM
aaron.ballman added inline comments.Nov 21 2017, 11:43 AM
lib/AST/ASTImporter.cpp
5622 ↗(On Diff #123792)

const auto *?

5631 ↗(On Diff #123792)

Remove spurious parens.

unittests/AST/ASTImporterTest.cpp
548 ↗(On Diff #123792)

Please add a value-dependent test as well.

a.sidorin added inline comments.Nov 22 2017, 2:49 AM
lib/AST/ASTImporter.cpp
5622 ↗(On Diff #123792)

By the way, you can replace the loop with ImportContainerChecked().

tk1012 updated this revision to Diff 124057.Nov 23 2017, 3:07 AM

Hello there,

I update the diff, reflecting the comments.

Updates:

  1. Use ImportContainerChecked() for importing TypeSourceInfo.
  2. Modify bool ToValue = (...) ? ... : false;.
  3. Add a test case for the value-dependent TypeTraitExpr.
tk1012 marked 4 inline comments as done.Nov 23 2017, 3:08 AM

Hello Takafumi,

This is almost OK to me but there is an inline comment we need to resolve in order to avoid Windows buildbot failures.
In addition, as Gabor pointed, when we add a new matcher, we need to update matcher documentation as well. To update the docs, you should perform just three steps.

  1. Rebase your patch onto latest master
  2. Launch script docs/tools/dump_ast_matchers.py
  3. Add changes made by this script to your commit.

After these changes are made, I will approve the patch. Thank you!

unittests/AST/ASTImporterTest.cpp
554 ↗(On Diff #124057)

Please add void f() { declToImport<int>().m(); } after declToImport` definition. The reason is that in MSVC mode, uninstantiated templates are ignored so the test will fail. We need this to avoid this.

aaron.ballman edited edge metadata.Nov 23 2017, 7:55 AM

Hello Takafumi,

This is almost OK to me but there is an inline comment we need to resolve in order to avoid Windows buildbot failures.
In addition, as Gabor pointed, when we add a new matcher, we need to update matcher documentation as well. To update the docs, you should perform just three steps.

  1. Rebase your patch onto latest master
  2. Launch script docs/tools/dump_ast_matchers.py
  3. Add changes made by this script to your commit.

I'm a bit confused -- I don't see any ASTMatcher changes as part of this patch aside from what's in the test file. Are you suggesting the matcher there should be hoisted as a separate patch?

Hello Takafumi,

This is almost OK to me but there is an inline comment we need to resolve in order to avoid Windows buildbot failures.
In addition, as Gabor pointed, when we add a new matcher, we need to update matcher documentation as well. To update the docs, you should perform just three steps.

  1. Rebase your patch onto latest master
  2. Launch script docs/tools/dump_ast_matchers.py
  3. Add changes made by this script to your commit.

I'm a bit confused -- I don't see any ASTMatcher changes as part of this patch aside from what's in the test file. Are you suggesting the matcher there should be hoisted as a separate patch?

In a previous version the matcher was public. I think it either should be public with documentation or it can remain in the test file and no doc update required. I am fine with both solution.

Oh, sorry! I was thinking that a matcher is still in ASTMatchers.h (in previous revisions it was there). If matcher is internal (current revision), there is no need to update docs.

Hello Takafumi,

This is almost OK to me but there is an inline comment we need to resolve in order to avoid Windows buildbot failures.
In addition, as Gabor pointed, when we add a new matcher, we need to update matcher documentation as well. To update the docs, you should perform just three steps.

  1. Rebase your patch onto latest master
  2. Launch script docs/tools/dump_ast_matchers.py
  3. Add changes made by this script to your commit.

I'm a bit confused -- I don't see any ASTMatcher changes as part of this patch aside from what's in the test file. Are you suggesting the matcher there should be hoisted as a separate patch?

In a previous version the matcher was public. I think it either should be public with documentation or it can remain in the test file and no doc update required. I am fine with both solution.

I'm fine with either exposing it as a public matcher or leaving it private for the test file, but if it gets exposed, I think it should be in a separate patch.

tk1012 updated this revision to Diff 124124.Nov 23 2017, 6:05 PM

Hello there,

I update the diff and add void f() { declToImport<int>().m(); } after declToImport definition.
I leave the matcher for private in the test file, so I don't update the documentation.

tk1012 marked an inline comment as done.Nov 23 2017, 6:05 PM
aaron.ballman added inline comments.Nov 24 2017, 10:15 AM
unittests/AST/ASTImporterTest.cpp
553 ↗(On Diff #124124)

Drop the spurious semicolon at the end of the function definition.

566 ↗(On Diff #124124)

Is this type actually correct for C++? I would expect that for C code, but not for C++.

tk1012 updated this revision to Diff 124232.Nov 24 2017, 10:38 AM

Hello Aaron,

I remove the semicolon.

Is this type actually correct for C++?

Yes, it is.
clang generates the AST for declToImport struct like this.

|-CXXRecordDecl 0x8b19fe0 <col:22, col:29> col:29 implicit struct declToImport
`-CXXMethodDecl 0x8b1a0d0 <line:2:3, col:27> col:8 m 'void (void)'
  `-CompoundStmt 0x8b1a1e8 <col:12, col:27>
    `-TypeTraitExpr 0x8b1a1c8 <col:14, col:24> '_Bool'
aaron.ballman accepted this revision.Nov 24 2017, 10:42 AM

Hello Aaron,

I remove the semicolon.

Is this type actually correct for C++?

Yes, it is.
clang generates the AST for declToImport struct like this.

|-CXXRecordDecl 0x8b19fe0 <col:22, col:29> col:29 implicit struct declToImport
`-CXXMethodDecl 0x8b1a0d0 <line:2:3, col:27> col:8 m 'void (void)'
  `-CompoundStmt 0x8b1a1e8 <col:12, col:27>
    `-TypeTraitExpr 0x8b1a1c8 <col:14, col:24> '_Bool'

Hmm, that looks like a bug to me; but not one that should impact this review.

LGTM!

Hello Aaron,

I remove the semicolon.

Is this type actually correct for C++?

Yes, it is.
clang generates the AST for declToImport struct like this.

|-CXXRecordDecl 0x8b19fe0 <col:22, col:29> col:29 implicit struct declToImport
`-CXXMethodDecl 0x8b1a0d0 <line:2:3, col:27> col:8 m 'void (void)'
  `-CompoundStmt 0x8b1a1e8 <col:12, col:27>
    `-TypeTraitExpr 0x8b1a1c8 <col:14, col:24> '_Bool'

Hmm, that looks like a bug to me; but not one that should impact this review.

Yeah, it seems as though the ASTDumper does not receive a printing policy, and the default behavior of QualType::getAsString() is to gin up a very dumb printing policy from default language options, hence the poor quality of the name here.

LGTM, thank you!

unittests/AST/ASTImporterTest.cpp
566 ↗(On Diff #124124)

hasType(booleanType())?

tk1012 updated this revision to Diff 124286.Nov 26 2017, 4:04 AM

Hello there,

I update the diff to follow your comments.

tk1012 marked 2 inline comments as done.Nov 26 2017, 4:04 AM
a.sidorin accepted this revision.Nov 26 2017, 4:13 AM

Thank you!

This revision was automatically updated to reflect the committed changes.