This is an archive of the discontinued LLVM Phabricator instance.

docs: add documentation describing API Notes
ClosedPublic

Authored by compnerd on Sep 28 2020, 1:47 PM.

Details

Summary

API Notes are a feature which allows annotation of headers by an
auxiliary file that contains metadata for declarations pertaining to the
associated module. This enables adding attributes to declarations
without requiring modification of the headers, enabling finer grained
control for library headers for consumers without having to modify
external headers.

Based on the work from https://github.com/llvm/llvm-project-staging/commit/d238492fd3a82160955d0642b85d8ce620fa3258

Diff Detail

Event Timeline

compnerd created this revision.Sep 28 2020, 1:47 PM
Herald added a project: Restricted Project. · View Herald TranscriptSep 28 2020, 1:47 PM
Herald added a subscriber: jfb. · View Herald Transcript
compnerd requested review of this revision.Sep 28 2020, 1:47 PM
keith added a subscriber: keith.Sep 28 2020, 3:06 PM

Broadly, it seems reasonable to me for Clang to support this. I have no major concerns with the overall approach here, and it seems like you already have sufficient implementation experience with this approach to know that it's going to work out well in practice. I'm happy that all of the points in http://clang.llvm.org/get_involved.html for Clang extensions are covered -- or will be covered by the work to implement this.

clang/docs/APINotes.rst
6–13

"API" should generally be used only as an uncountable noun. If you mean something more concrete here, such as "a function, method, type, ...", then saying that would be better, since "API" can be interpreted as meaning an individual such entity or a collection of such entities, depending on the reader.

46

Can we add a hyphen between api and notes here? -fapinotes is a little hard to read.

188–191

Is it important that these are single letters? Spelling out the name in full (as you do for other enumerated values like MethodKind and PropertyKind) would seem a little more readable. (We could accept the single-letter forms as aliases.)

234–236

So what context is it parsed in? Below you have an NSArray * example; how do we do the lookup for NSArray?

274
compnerd marked 3 inline comments as done.Oct 1 2020, 8:30 AM
compnerd added inline comments.
clang/docs/APINotes.rst
46

Sure; we can always add a -fapinotes-modules as a silent alias for backwards compatibility. I assume you would like the same for the rest of the options, and have made that change through out.

188–191

I don't think that they be single letters is important. As long as we have the compatibility aliases, I think it should be fine to support the fully spelt out versions, as the compatibility is needed for existing APINotes.

234–236

A separate buffer is constructed where the annotations are processed. During semantic analysis, the requested APINotes are processed and the attributes specified are applied to the declaration.

hlopko accepted this revision.Oct 1 2020, 11:43 PM
hlopko added a subscriber: hlopko.

Thanks for pushing this forward!

Current proposal seems to be only dealing with annotating ObjC for Swift, but API notes seem like a feature that would be useful for other languages as well. I briefly chatted with gribozavr and we concluded that if the need arises, the current design should be extensible to e.g. C++ (one of the bigger problems being how to name C++ function/overload sets).

clang/docs/APINotes.rst
188–191

+1, this is the only attribute that is abbreviated.

217

Could we get more context from the Apple bug tracker? Depending on the details it might be reasonable to drop NullabilityOfRet attribute completely, or more precisely document when it should be used.

This revision is now accepted and ready to land.Oct 1 2020, 11:43 PM
compnerd marked an inline comment as done.Oct 2 2020, 8:31 AM
compnerd added inline comments.
clang/docs/APINotes.rst
217

The bug is that Nullability and Type on Parameters entries don’t really compose. Consider the following note:

NullabilityOfRet: N
Parameters:
- Position: 1
  Nullability: O
  Type: 'NSDictionary<NSString *, id> *'

on a method: this parameter won’t be treated as optional. Effectively, you need to write the _Nullable on the type because the Nullability entry is ignored.

I think that fixing this later is preferable rather than earlier to ensure that we at least have a unified starting point where clang and Swift can co-evolve. If we try to do that while upstreaming, it will make everything far more difficult because you have to coordinate over multiple (3+) repositories.

compnerd updated this revision to Diff 295835.Oct 2 2020, 8:37 AM

Update text based on feedback.

rsmith added inline comments.Oct 2 2020, 2:31 PM
clang/docs/APINotes.rst
234–236

I've not been able to work out what the rule is, based on this documentation. The name NSArray isn't predeclared, so how is it found? How can a user ensure their types are visible to this parsing process? Are these things parsed as if they appear at the global scope in a translation unit in which the corresponding framework has been imported? (And if so, why wouldn't macros be available there too?) Whatever the rule is, I'd like to see it documented.

compnerd marked 2 inline comments as done.Oct 2 2020, 3:46 PM
compnerd added inline comments.
clang/docs/APINotes.rst
234–236

Discussed offline, this has been updated.

compnerd updated this revision to Diff 295924.Oct 2 2020, 3:47 PM
compnerd marked an inline comment as done.

Address feedback from @rsmith

rsmith accepted this revision.Oct 2 2020, 3:49 PM

Thanks, looks good to me.

clang/docs/APINotes.rst
251–253

Please update this text similarly.

This revision was landed with ongoing or failed builds.Oct 5 2020, 11:30 AM
This revision was automatically updated to reflect the committed changes.