[cfe-commits] r94999 - in /cfe/trunk: include/clang/AST/Decl.h lib/AST/Decl.cpp lib/AST/Expr.cpp lib/AST/ExprConstant.cpp lib/CodeGen/CodeGenModule.cpp lib/Sema/SemaChecking.cpp lib/Sema/SemaDecl.cpp lib/Sema/SemaDeclCXX.cpp tools/CIndex/CIndex.cpp
Sebastian Redl
sebastian.redl at getdesigned.at
Mon Feb 1 12:16:42 PST 2010
Author: cornedbee
Date: Mon Feb 1 14:16:42 2010
New Revision: 94999
URL: http://llvm.org/viewvc/llvm-project?rev=94999&view=rev
Log:
In C++, an initializer on a variable doesn't necessarily mean it's the definition. With that in mind, rename getDefinition to getAnyInitializer (to distinguish it from getInit) and reimplement it in terms of isThisDeclarationADefinition. Update all code to use this new function.
Modified:
cfe/trunk/include/clang/AST/Decl.h
cfe/trunk/lib/AST/Decl.cpp
cfe/trunk/lib/AST/Expr.cpp
cfe/trunk/lib/AST/ExprConstant.cpp
cfe/trunk/lib/CodeGen/CodeGenModule.cpp
cfe/trunk/lib/Sema/SemaChecking.cpp
cfe/trunk/lib/Sema/SemaDecl.cpp
cfe/trunk/lib/Sema/SemaDeclCXX.cpp
cfe/trunk/tools/CIndex/CIndex.cpp
Modified: cfe/trunk/include/clang/AST/Decl.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Decl.h?rev=94999&r1=94998&r2=94999&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Decl.h (original)
+++ cfe/trunk/include/clang/AST/Decl.h Mon Feb 1 14:16:42 2010
@@ -560,22 +560,19 @@
/// \brief Get the tentative definition that acts as the real definition in
/// a TU. Returns null if there is a proper definition available.
- const VarDecl *getActingDefinition() const;
VarDecl *getActingDefinition();
+ const VarDecl *getActingDefinition() const {
+ return const_cast<VarDecl*>(this)->getActingDefinition();
+ }
/// \brief Determine whether this is a tentative definition of a
/// variable in C.
bool isTentativeDefinitionNow() const;
- /// \brief Retrieve the definition of this variable, which may come
- /// from a previous declaration. Def will be set to the VarDecl that
- /// contains the initializer, and the result will be that
- /// initializer.
- const Expr *getDefinition(const VarDecl *&Def) const;
-
- const Expr *getDefinition() const {
- const VarDecl* Definition;
- return getDefinition(Definition);
+ /// \brief Get the real (not just tentative) definition for this declaration.
+ VarDecl *getDefinition();
+ const VarDecl *getDefinition() const {
+ return const_cast<VarDecl*>(this)->getDefinition();
}
/// \brief Determine whether this is or was instantiated from an out-of-line
@@ -600,6 +597,17 @@
return false;
}
+ /// getAnyInitializer - Get the initializer for this variable, no matter which
+ /// declaration it is attached to.
+ const Expr *getAnyInitializer() const {
+ const VarDecl *D;
+ return getAnyInitializer(D);
+ }
+
+ /// getAnyInitializer - Get the initializer for this variable, no matter which
+ /// declaration it is attached to. Also get that declaration.
+ const Expr *getAnyInitializer(const VarDecl *&D) const;
+
bool hasInit() const {
return !Init.isNull();
}
Modified: cfe/trunk/lib/AST/Decl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Decl.cpp?rev=94999&r1=94998&r2=94999&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Decl.cpp (original)
+++ cfe/trunk/lib/AST/Decl.cpp Mon Feb 1 14:16:42 2010
@@ -522,10 +522,6 @@
return Definition;
}
-const VarDecl *VarDecl::getActingDefinition() const {
- return const_cast<VarDecl*>(this)->getActingDefinition();
-}
-
VarDecl *VarDecl::getActingDefinition() {
DefinitionKind Kind = isThisDeclarationADefinition();
if (Kind != TentativeDefinition)
@@ -553,16 +549,24 @@
if ((*I)->isThisDeclarationADefinition() == Definition)
return false;
}
- return true;
+ return true;
+}
+
+VarDecl *VarDecl::getDefinition() {
+ for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
+ if ((*I)->isThisDeclarationADefinition() == Definition)
+ return *I;
+ }
+ return 0;
}
-const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
+const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
redecl_iterator I = redecls_begin(), E = redecls_end();
while (I != E && !I->getInit())
++I;
if (I != E) {
- Def = *I;
+ D = *I;
return I->getInit();
}
return 0;
Modified: cfe/trunk/lib/AST/Expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Expr.cpp?rev=94999&r1=94998&r2=94999&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Expr.cpp (original)
+++ cfe/trunk/lib/AST/Expr.cpp Mon Feb 1 14:16:42 2010
@@ -99,8 +99,7 @@
else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
if (Var->getType()->isIntegralType() &&
Var->getType().getCVRQualifiers() == Qualifiers::Const) {
- const VarDecl *Def = 0;
- if (const Expr *Init = Var->getDefinition(Def))
+ if (const Expr *Init = Var->getAnyInitializer())
if (Init->isValueDependent())
ValueDependent = true;
}
@@ -1654,15 +1653,14 @@
if (Quals.hasVolatile() || !Quals.hasConst())
return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
- // Look for the definition of this variable, which will actually have
- // an initializer.
- const VarDecl *Def = 0;
- const Expr *Init = Dcl->getDefinition(Def);
+ // Look for a declaration of this variable that has an initializer.
+ const VarDecl *ID = 0;
+ const Expr *Init = Dcl->getAnyInitializer(ID);
if (Init) {
- if (Def->isInitKnownICE()) {
+ if (ID->isInitKnownICE()) {
// We have already checked whether this subexpression is an
// integral constant expression.
- if (Def->isInitICE())
+ if (ID->isInitICE())
return NoDiag();
else
return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
@@ -1674,7 +1672,7 @@
// specify a constant-initializer which shall be an integral
// constant expression (5.19). In that case, the member can appear
// in integral constant expressions.
- if (Def->isOutOfLine()) {
+ if (ID->isOutOfLine()) {
Dcl->setInitKnownICE(false);
return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
}
Modified: cfe/trunk/lib/AST/ExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=94999&r1=94998&r2=94999&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ExprConstant.cpp (original)
+++ cfe/trunk/lib/AST/ExprConstant.cpp Mon Feb 1 14:16:42 2010
@@ -263,8 +263,7 @@
if (!VD->getType()->isReferenceType())
return APValue(E);
// FIXME: Check whether VD might be overridden!
- const VarDecl *Def = 0;
- if (const Expr *Init = VD->getDefinition(Def))
+ if (const Expr *Init = VD->getAnyInitializer())
return Visit(const_cast<Expr *>(Init));
}
@@ -880,8 +879,7 @@
if (Info.Ctx.getCanonicalType(E->getType()).getCVRQualifiers()
== Qualifiers::Const) {
if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
- const VarDecl *Def = 0;
- if (const Expr *Init = VD->getDefinition(Def)) {
+ if (const Expr *Init = VD->getAnyInitializer()) {
if (APValue *V = VD->getEvaluatedValue()) {
if (V->isInt())
return Success(V->getInt(), E);
Modified: cfe/trunk/lib/CodeGen/CodeGenModule.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=94999&r1=94998&r2=94999&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Mon Feb 1 14:16:42 2010
@@ -979,7 +979,7 @@
QualType ASTTy = D->getType();
bool NonConstInit = false;
- const Expr *InitExpr = D->getDefinition();
+ const Expr *InitExpr = D->getAnyInitializer();
if (!InitExpr) {
// This is a tentative definition; tentative definitions are
Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=94999&r1=94998&r2=94999&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Mon Feb 1 14:16:42 2010
@@ -846,8 +846,7 @@
}
if (isConstant) {
- const VarDecl *Def = 0;
- if (const Expr *Init = VD->getDefinition(Def))
+ if (const Expr *Init = VD->getAnyInitializer())
return SemaCheckStringLiteral(Init, TheCall,
HasVAListArg, format_idx, firstDataArg);
}
Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=94999&r1=94998&r2=94999&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Mon Feb 1 14:16:42 2010
@@ -2360,9 +2360,9 @@
// attributes declared post-definition are currently ignored
if (Previous.isSingleResult()) {
- const VarDecl *Def = 0;
- VarDecl *PrevDecl = dyn_cast<VarDecl>(Previous.getFoundDecl());
- if (PrevDecl && PrevDecl->getDefinition(Def) && D.hasAttributes()) {
+ VarDecl *Def = dyn_cast<VarDecl>(Previous.getFoundDecl());
+ if (Def && (Def = Def->getDefinition()) &&
+ Def != NewVD && D.hasAttributes()) {
Diag(NewVD->getLocation(), diag::warn_attribute_precede_definition);
Diag(Def->getLocation(), diag::note_previous_definition);
}
@@ -3435,8 +3435,8 @@
AbstractVariableType))
VDecl->setInvalidDecl();
- const VarDecl *Def = 0;
- if (VDecl->getDefinition(Def)) {
+ const VarDecl *Def;
+ if ((Def = VDecl->getDefinition()) && Def != VDecl) {
Diag(VDecl->getLocation(), diag::err_redefinition)
<< VDecl->getDeclName();
Diag(Def->getLocation(), diag::note_previous_definition);
Modified: cfe/trunk/lib/Sema/SemaDeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclCXX.cpp?rev=94999&r1=94998&r2=94999&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Mon Feb 1 14:16:42 2010
@@ -4062,8 +4062,8 @@
AbstractVariableType))
VDecl->setInvalidDecl();
- const VarDecl *Def = 0;
- if (VDecl->getDefinition(Def)) {
+ const VarDecl *Def;
+ if ((Def = VDecl->getDefinition()) && Def != VDecl) {
Diag(VDecl->getLocation(), diag::err_redefinition)
<< VDecl->getDeclName();
Diag(Def->getLocation(), diag::note_previous_definition);
Modified: cfe/trunk/tools/CIndex/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndex.cpp?rev=94999&r1=94998&r2=94999&view=diff
==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndex.cpp Mon Feb 1 14:16:42 2010
@@ -1733,23 +1733,10 @@
}
case Decl::Var: {
- VarDecl *Var = cast<VarDecl>(D);
-
- // Variables with initializers have definitions.
- const VarDecl *Def = 0;
- if (Var->getDefinition(Def))
- return MakeCXCursor(const_cast<VarDecl *>(Def), CXXUnit);
-
- // extern and private_extern variables are not definitions.
- if (Var->hasExternalStorage())
- return clang_getNullCursor();
-
- // In-line static data members do not have definitions.
- if (Var->isStaticDataMember() && !Var->isOutOfLine())
- return clang_getNullCursor();
-
- // All other variables are themselves definitions.
- return C;
+ // Ask the variable if it has a definition.
+ if (VarDecl *Def = cast<VarDecl>(D)->getDefinition())
+ return MakeCXCursor(Def, CXXUnit);
+ return clang_getNullCursor();
}
case Decl::FunctionTemplate: {
More information about the cfe-commits
mailing list