[PATCH] D28526: [ARM] Add diagnostics when initialization global variables with ropi/rwpi

Eli Friedman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 10 12:55:47 PST 2017


efriedma requested changes to this revision.
efriedma added a reviewer: efriedma.
efriedma added a comment.
This revision now requires changes to proceed.

We really should be checking this much earlier.

IsGlobalLValue() in ExprConstant.cpp is the canonical place to answer the question "is the address of the given expression constant".  Getting this right in semantic analysis means you don't have to chase down all the various places which generate global initializers.  In addition to global variables, static local variables and compound literals can have static storage duration in C.  And sometimes you can even run into trouble with globals which don't exist in the source code.  One interesting example:

  int a;
  int g(const int**);
  int f(void) {
    const int *x[] = {&a,&a,&a,&a,&a,&a,&a,&a};
    return g(x);
  }

This is valid code whether or not we're in rwpi mode, but clang currently generates IR which won't link in rwpi mode.  If semantic analysis concludes that `&a` isn't a constant, the correct code generation falls out naturally; otherwise, you have yet another place to add an explicit check in IR generation.  And I won't even try to go into all the additional corner cases you can run into with C++ code.

As a bonus, you can generate nicer error messages in Sema.


https://reviews.llvm.org/D28526





More information about the cfe-commits mailing list