This is an archive of the discontinued LLVM Phabricator instance.

Treat instantiations of variable templates as definitions
AbandonedPublic

Authored by sepavloff on Sep 16 2016, 2:35 AM.

Details

Reviewers
None
Summary

Current version of clang cannot build the program:

template<int n> int var;
int main(int argc, char *argv[]) {
  return var<0>;
}

as linker does not find var<0>, codegen treats it as declaration only.
However this program must build succesfully, because the template
declaration is a definition as none of the conditions mentioned in
[basic.def]p2 is met. With this change codegen generates definitions
for file level variable template specialization even if the declaration
does not contain an initializer.

Diff Detail

Event Timeline

sepavloff updated this revision to Diff 71603.Sep 16 2016, 2:35 AM
sepavloff retitled this revision from to Treat instantiations of variable templates as definitions.
sepavloff updated this object.
sepavloff added reviewers: rsmith, tra.
sepavloff added a subscriber: cfe-commits.
tra removed a reviewer: tra.Sep 28 2016, 11:22 AM
tra added a subscriber: tra.
rsmith added inline comments.Sep 28 2016, 11:46 AM
lib/CodeGen/CodeGenModule.cpp
1612–1616

It's not appropriate to work around this in CodeGen. Instead, we need to fix the AST representation to distinguish between a variable template specialization definition with no initializer and a variable template specialization declaration. I'd suggest adding a flag to the AST to mark that a variable is not actually a definition despite looking like one, and use that to flag declaration-only instantiations of variable template specializations. See also D24508, where we have another use case for such a flag.

sepavloff removed subscribers: tra, cfe-commits.
sepavloff abandoned this revision.Aug 8 2018, 12:03 AM

Fixed in r319605.