This is an archive of the discontinued LLVM Phabricator instance.

[clang][ASTImporter] Fix an import error handling related bug.
ClosedPublic

Authored by balazske on Mar 26 2022, 3:10 AM.

Details

Summary

This bug can cause that more import errors are generated than necessary
and many objects fail to import. Chance of an invalid AST after these
imports increases.

Diff Detail

Event Timeline

balazske created this revision.Mar 26 2022, 3:10 AM
Herald added a reviewer: shafik. · View Herald Transcript
Herald added a project: Restricted Project. · View Herald Transcript
balazske requested review of this revision.Mar 26 2022, 3:10 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 26 2022, 3:10 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

Nice catch!

clang/lib/AST/ASTImporter.cpp
8789–8791

I think, this comment would be better to describe the declaration of PrevFromDi.

8792–8794

We should elevate this logic and the one in ImportDeclContext that uses consumeError into a common abstraction. I.e. making them closer to each other in a class.
Something like (draft, did not test it, might not compile):

class ErrorHandlingStrategy {
  static bool accumulateChildErrors(DeclContext*);
  static bool accumulateChildErrors(Decl *FromDi, Decl* Parent); // Use this one when traversing through the import path!
public:
  static Error handleDeclContextError(Error Err, DeclContext* FromDC) {
    Error ChildErrors = Error::success();
            if (Err && accumulateChildErrors(FromDC))
              ChildErrors =  joinErrors(std::move(ChildErrors), std::move(Err));
            else
              consumeError(std::move(Err));
     return ChildErrors
  }
  
};
balazske added inline comments.Apr 6 2022, 7:41 AM
clang/lib/AST/ASTImporter.cpp
8792–8794

The AccumulateChildErrors value looks the only common thing. It can mean: Accumulate errors (or not) for declarations that are in child-relationship with the parent. The value is only dependent on the parent, true if it is a TagDecl. This value can be extracted into a function that is used in importDeclContext and at Import(Decl *). At importDeclContext we should accumulate or discard errors for every child node. But not for possible other nodes that are imported but are not a child node (probably no such exists now but theoretically it is possible, and when using the error handling strategy for non-namespace-like nodes).

martong added inline comments.Apr 11 2022, 2:43 AM
clang/lib/AST/ASTImporter.cpp
8792–8794

My point is to try to encapsulate the error handling logic as much as possible. It is not just about AccumulateChildErrors. The current change scatters the error handling logic into seemingly distant code parts. The ErrorHandlingStrategy class could serve this encapsulation, because that's what classes do.

balazske updated this revision to Diff 422139.Apr 12 2022, 1:18 AM

Created a common place for the DeclContext child error handling code.

martong accepted this revision.Apr 12 2022, 7:00 AM

Very good! Thanks for the update!

This revision is now accepted and ready to land.Apr 12 2022, 7:00 AM
balazske marked 3 inline comments as done.Apr 13 2022, 12:41 AM
This revision was landed with ongoing or failed builds.Apr 13 2022, 1:12 AM
This revision was automatically updated to reflect the committed changes.