This is an archive of the discontinued LLVM Phabricator instance.

Use virtual functions in ParsedAttrInfo instead of function pointers
ClosedPublic

Authored by john.brawn on Mar 24 2017, 9:24 AM.

Details

Summary

This doesn't do anything on its own, but it's the first step towards allowing plugins to define attributes. It does simplify the ParsedAttrInfo generation in ClangAttrEmitter a little though.

Diff Detail

Event Timeline

john.brawn set the repository for this revision to rG LLVM Github Monorepo.
john.brawn added a subscriber: llvm-commits.

Resurrecting this old patch, with the intention of trying to get it committed this time. This is the first of four patches to make it possible for clang plugins to define attributes.

Herald added a project: Restricted Project. · View Herald TranscriptJan 21 2020, 6:02 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
erichkeane added inline comments.Jan 21 2020, 7:11 AM
clang/lib/Sema/ParsedAttr.cpp
144

I don't think you can do this. The only way to get Info as nullptr is to end up off the end of the array, which is UB.

Instead of the map lookup, this should check A.getKind against the range.

147

Currently the behavior of this is to make an invalid attribute kind be UB. Presumably the point here is to make it so that is no longer the case?

clang/utils/TableGen/ClangAttrEmitter.cpp
3410

Why does this take SS AND OS. What is the difference here? Does this actually use OS anymore?

3653

Should the instance be static? Perhaps a static variable in the class itself?

3656–3657

Is there a good reason this doesn't return references?

john.brawn marked 4 inline comments as done.Jan 21 2020, 10:30 AM
john.brawn added inline comments.
clang/lib/Sema/ParsedAttr.cpp
144

AttrInfoMap is declared to have size ParsedAttr::UnknownAttribute+1, so when the kind is too large we get an element that wasn't explicitly initialized so will be null.

Doing it by kind makes more sense though, so I'll do that.

clang/utils/TableGen/ClangAttrEmitter.cpp
3410

It's because of GenerateCustomAppertainsTo. That generates a function that expects to be at file scope (because emitAttributeMatchRules re-uses the same function), but at the time GenerateAppertainsTo is called SS is in the middle of the ParsedAttrInfo. Previously both the function that GenerateCustomAppertainsTo generates and that this file generates would be at file scope, so both were written to OS.

3653

Making it static makes sense, I'll do that.

3656–3657

You can't have an array of references (C++11 dcl.ref paragraph 5).

erichkeane added inline comments.Jan 21 2020, 10:33 AM
clang/utils/TableGen/ClangAttrEmitter.cpp
3410

Thanks!

3653

static class member is what I'm thinking. That way it is ParsedAttrInfoWHATEVER::Instance?

3656–3657

Ah, right... got lost in the code-that-generates-code thing.

Update based on review comments. Also fix warnings due to missing virtual destructor that I hadn't noticed before.

carwil added a subscriber: carwil.Jan 27 2020, 2:13 AM
aaron.ballman added inline comments.Feb 2 2020, 10:01 AM
clang/lib/Sema/ParsedAttr.cpp
141

Might as well lower this variable into the function -- no real need for it to have file scope.

clang/utils/TableGen/ClangAttrEmitter.cpp
3410

I think the stream parameter names should be a bit more descriptive here (and perhaps some comments on the function would be a good idea as well). Perhaps FileScopeStream and LexicalScopeStream?

john.brawn marked 2 inline comments as done.Feb 4 2020, 8:17 AM
john.brawn added inline comments.
clang/lib/Sema/ParsedAttr.cpp
141

Sure.

clang/utils/TableGen/ClangAttrEmitter.cpp
3410

Actually, looking at this again there's no reason we can't generate the CustomApperatinsTo functions in a loop at the start of EmitClangAttrParsedAttrImpl. That way everything can just go to a single output stream and we don't have this problem. I'll rearrange things to do that instead.

john.brawn updated this revision to Diff 242363.Feb 4 2020, 9:53 AM

Move DefaultParsedAttrInfo out of file scope, and move custom appertains-to function generation out of the loop in EmitClangAttrParsedAttrImpl so we only need to use one output stream.

aaron.ballman accepted this revision.Feb 23 2020, 7:11 AM

LGTM aside from a minor nit and a question.

clang/lib/Sema/ParsedAttr.cpp
144

You can use llvm::array_lengthof() here instead.

clang/utils/TableGen/ClangAttrEmitter.cpp
3653

Would it make sense for this object to be const under the assumption that once we've generated a ParsedAttrInfo object, we don't want to modify its properties? I'm not certain if trying to be const-correct here would be a burden or not, so I don't insist on a change unless it's trivial to support.

This revision is now accepted and ready to land.Feb 23 2020, 7:11 AM
john.brawn marked 2 inline comments as done.Feb 26 2020, 6:39 AM
john.brawn added inline comments.
clang/lib/Sema/ParsedAttr.cpp
144

Will do.

clang/utils/TableGen/ClangAttrEmitter.cpp
3653

Making this const actually is trivial, so I'll do that.

This revision was automatically updated to reflect the committed changes.