This is an archive of the discontinued LLVM Phabricator instance.

[clang] Omit most AttributedStatements from the CFG
ClosedPublic

Authored by thakis on Oct 11 2021, 11:21 AM.

Details

Summary

[[clang::fallthrough]] has meaning for the CFG, but all other
StmtAttrs we currently have don't. So omit them, as AttributedStatements
with children cause several issues and there's no benefit in including
them.

Fixes PR52103 and PR49454. See PR52103 for details.

Diff Detail

Event Timeline

thakis requested review of this revision.Oct 11 2021, 11:21 AM
thakis created this revision.
thakis added inline comments.Oct 11 2021, 11:32 AM
clang/lib/Analysis/CFG.cpp
2410

(Compare this function to CFGBuilder::VisitStmt in this file, which is the default function that's called for Stmts and which is what was called for AttributedStmt before this patch added a dedicated handler for AttributedStmt.)

Thank you for the fix to this!

clang/lib/Analysis/CFG.cpp
2418–2420

What about OpenCLUnrollHintAttr, NoMergeAttr, and MustTailAttr? These all have some semantic effect as statement attributes in terms of changing codegen, but perhaps they don't need modelling in the CFG?

(I'm trying to decide whether we may want to tablegen this functionality and so we might want something more general than isFallthroughStatement().)

thakis added inline comments.Oct 11 2021, 1:33 PM
clang/lib/Analysis/CFG.cpp
2418–2420

Right, I think they all have no interesting effect on the CFG.

It's hard to predict the future, so I'd say let's wait and see until there are more StmtAttrs :)

hans added inline comments.Oct 12 2021, 4:40 AM
clang/lib/Analysis/CFG.cpp
545

Change param name C -> A as in the function definition?

2407

Can fallthrough statements ever have children? If not, should it be an assert instead of a check here?

thakis marked an inline comment as done.Oct 12 2021, 5:53 AM
thakis added inline comments.
clang/lib/Analysis/CFG.cpp
2407

Good question. Attr.td says:

// The attribute only applies to a NullStmt, but we have special fix-it
// behavior if applied to a case label.
let Subjects = SubjectList<[NullStmt, SwitchCase], ErrorDiag,
                           "empty statements">;

Which I suppose triggers for this:

switch (argc) {
  [[fallthrough]] case 4:
    break;
}
foo.cc:6:7: error: 'fallthrough' attribute is only allowed on empty statements
    [[fallthrough]] case 4:
      ^             ~~~~
foo.cc:6:20: note: did you forget ';'?
    [[fallthrough]] case 4:

But that doesn't seem to make it into the AST, according to -dump-ast. So I suppose it could be an assert as well. Want me to change this?

hans accepted this revision.Oct 12 2021, 5:59 AM
hans added inline comments.
clang/lib/Analysis/CFG.cpp
2407

Yes, I think an assert would make sense, otherwise the reader has to think about what would the code be doing for an AttributedStmt with non-null substmt.

This revision is now accepted and ready to land.Oct 12 2021, 5:59 AM
thakis updated this revision to Diff 378996.Oct 12 2021, 6:03 AM

rename param; assert isa<NullStmt>

This revision was landed with ongoing or failed builds.Oct 12 2021, 6:15 AM
This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptOct 12 2021, 6:15 AM

LGTM, thank you @thakis!

clang/lib/Analysis/CFG.cpp
2407

I think it's fine to assert because we're required to reject fallthrough statements applying to something other than a null statement: https://eel.is/c++draft/dcl.attr.fallthrough#1.sentence-1

2418–2420

SGTM!