Skip to content
This repository has been archived by the owner on Apr 23, 2020. It is now read-only.

Commit

Permalink
Improve location fidelity of objc decls.
Browse files Browse the repository at this point in the history
-Add the location of the class name to all objc container decls, not just ObjCInterfaceDecl.
-Make objc decls consistent with the rest of the NamedDecls and have getLocation() point to the
 class name, not the location of '@'.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@141061 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
akyrtzi committed Oct 4, 2011
1 parent 55d78d2 commit 1711fc9
Show file tree
Hide file tree
Showing 15 changed files with 144 additions and 136 deletions.
80 changes: 42 additions & 38 deletions include/clang/AST/DeclObjC.h
Original file line number Diff line number Diff line change
Expand Up @@ -412,14 +412,17 @@ class ObjCMethodDecl : public NamedDecl, public DeclContext {
/// ObjCProtocolDecl, and ObjCImplDecl.
///
class ObjCContainerDecl : public NamedDecl, public DeclContext {
SourceLocation AtStart;

// These two locations in the range mark the end of the method container.
// The first points to the '@' token, and the second to the 'end' token.
SourceRange AtEnd;
public:

ObjCContainerDecl(Kind DK, DeclContext *DC, SourceLocation L,
IdentifierInfo *Id)
: NamedDecl(DK, DC, L, Id), DeclContext(DK) {}
ObjCContainerDecl(Kind DK, DeclContext *DC,
IdentifierInfo *Id, SourceLocation nameLoc,
SourceLocation atStartLoc)
: NamedDecl(DK, DC, nameLoc, Id), DeclContext(DK), AtStart(atStartLoc) {}

// Iterator access to properties.
typedef specific_decl_iterator<ObjCPropertyDecl> prop_iterator;
Expand Down Expand Up @@ -471,6 +474,9 @@ class ObjCContainerDecl : public NamedDecl, public DeclContext {

ObjCPropertyDecl *FindPropertyDeclaration(IdentifierInfo *PropertyId) const;

SourceLocation getAtStartLoc() const { return AtStart; }
void setAtStartLoc(SourceLocation Loc) { AtStart = Loc; }

// Marks the end of the container.
SourceRange getAtEndRange() const {
return AtEnd;
Expand All @@ -480,7 +486,7 @@ class ObjCContainerDecl : public NamedDecl, public DeclContext {
}

virtual SourceRange getSourceRange() const {
return SourceRange(getLocation(), getAtEndRange().getEnd());
return SourceRange(AtStart, getAtEndRange().getEnd());
}

// Implement isa/cast/dyncast/etc.
Expand Down Expand Up @@ -553,7 +559,6 @@ class ObjCInterfaceDecl : public ObjCContainerDecl {
/// completed by the external AST source when required.
mutable bool ExternallyCompleted : 1;

SourceLocation ClassLoc; // location of the class identifier.
SourceLocation SuperClassLoc; // location of the super class identifier.
SourceLocation EndLoc; // marks the '>', '}', or identifier.

Expand Down Expand Up @@ -748,12 +753,10 @@ class ObjCInterfaceDecl : public ObjCContainerDecl {
ObjCMethodDecl *lookupPrivateMethod(const Selector &Sel, bool Instance=true);

// Location information, modeled after the Stmt API.
SourceLocation getLocStart() const { return getLocation(); } // '@'interface
SourceLocation getLocStart() const { return getAtStartLoc(); } // '@'interface
SourceLocation getLocEnd() const { return EndLoc; }
void setLocEnd(SourceLocation LE) { EndLoc = LE; }

void setClassLoc(SourceLocation Loc) { ClassLoc = Loc; }
SourceLocation getClassLoc() const { return ClassLoc; }
void setSuperClassLoc(SourceLocation Loc) { SuperClassLoc = Loc; }
SourceLocation getSuperClassLoc() const { return SuperClassLoc; }

Expand Down Expand Up @@ -911,14 +914,17 @@ class ObjCProtocolDecl : public ObjCContainerDecl {

SourceLocation EndLoc; // marks the '>' or identifier.

ObjCProtocolDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id)
: ObjCContainerDecl(ObjCProtocol, DC, L, Id),
ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
SourceLocation nameLoc, SourceLocation atStartLoc)
: ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc),
isForwardProtoDecl(true) {
}

public:
static ObjCProtocolDecl *Create(ASTContext &C, DeclContext *DC,
SourceLocation L, IdentifierInfo *Id);
IdentifierInfo *Id,
SourceLocation nameLoc,
SourceLocation atStartLoc);

const ObjCProtocolList &getReferencedProtocols() const {
return ReferencedProtocols;
Expand Down Expand Up @@ -958,7 +964,7 @@ class ObjCProtocolDecl : public ObjCContainerDecl {
void setForwardDecl(bool val) { isForwardProtoDecl = val; }

// Location information, modeled after the Stmt API.
SourceLocation getLocStart() const { return getLocation(); } // '@'protocol
SourceLocation getLocStart() const { return getAtStartLoc(); } // '@'protocol
SourceLocation getLocEnd() const { return EndLoc; }
void setLocEnd(SourceLocation LE) { EndLoc = LE; }

Expand Down Expand Up @@ -1081,19 +1087,16 @@ class ObjCCategoryDecl : public ObjCContainerDecl {

/// true of class extension has at least one bitfield ivar.
bool HasSynthBitfield : 1;

/// \brief The location of the '@' in '@interface'
SourceLocation AtLoc;

/// \brief The location of the category name in this declaration.
SourceLocation CategoryNameLoc;

ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc,
SourceLocation ClassNameLoc, SourceLocation CategoryNameLoc,
IdentifierInfo *Id, ObjCInterfaceDecl *IDecl)
: ObjCContainerDecl(ObjCCategory, DC, ClassNameLoc, Id),
: ObjCContainerDecl(ObjCCategory, DC, Id, ClassNameLoc, AtLoc),
ClassInterface(IDecl), NextClassCategory(0), HasSynthBitfield(false),
AtLoc(AtLoc), CategoryNameLoc(CategoryNameLoc) {
CategoryNameLoc(CategoryNameLoc) {
}
public:

Expand Down Expand Up @@ -1155,17 +1158,10 @@ class ObjCCategoryDecl : public ObjCContainerDecl {
bool ivar_empty() const {
return ivar_begin() == ivar_end();
}

SourceLocation getAtLoc() const { return AtLoc; }
void setAtLoc(SourceLocation At) { AtLoc = At; }

SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; }
void setCategoryNameLoc(SourceLocation Loc) { CategoryNameLoc = Loc; }

virtual SourceRange getSourceRange() const {
return SourceRange(AtLoc, getAtEndRange().getEnd());
}

static bool classof(const Decl *D) { return classofKind(D->getKind()); }
static bool classof(const ObjCCategoryDecl *D) { return true; }
static bool classofKind(Kind K) { return K == ObjCCategory; }
Expand All @@ -1179,10 +1175,12 @@ class ObjCImplDecl : public ObjCContainerDecl {
ObjCInterfaceDecl *ClassInterface;

protected:
ObjCImplDecl(Kind DK, DeclContext *DC, SourceLocation L,
ObjCInterfaceDecl *classInterface)
: ObjCContainerDecl(DK, DC, L,
classInterface? classInterface->getIdentifier() : 0),
ObjCImplDecl(Kind DK, DeclContext *DC,
ObjCInterfaceDecl *classInterface,
SourceLocation nameLoc, SourceLocation atStartLoc)
: ObjCContainerDecl(DK, DC,
classInterface? classInterface->getIdentifier() : 0,
nameLoc, atStartLoc),
ClassInterface(classInterface) {}

public:
Expand Down Expand Up @@ -1239,13 +1237,17 @@ class ObjCCategoryImplDecl : public ObjCImplDecl {
// Category name
IdentifierInfo *Id;

ObjCCategoryImplDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
ObjCInterfaceDecl *classInterface)
: ObjCImplDecl(ObjCCategoryImpl, DC, L, classInterface), Id(Id) {}
ObjCCategoryImplDecl(DeclContext *DC, IdentifierInfo *Id,
ObjCInterfaceDecl *classInterface,
SourceLocation nameLoc, SourceLocation atStartLoc)
: ObjCImplDecl(ObjCCategoryImpl, DC, classInterface, nameLoc, atStartLoc),
Id(Id) {}
public:
static ObjCCategoryImplDecl *Create(ASTContext &C, DeclContext *DC,
SourceLocation L, IdentifierInfo *Id,
ObjCInterfaceDecl *classInterface);
IdentifierInfo *Id,
ObjCInterfaceDecl *classInterface,
SourceLocation nameLoc,
SourceLocation atStartLoc);

/// getIdentifier - Get the identifier that names the category
/// interface associated with this implementation.
Expand Down Expand Up @@ -1323,17 +1325,19 @@ class ObjCImplementationDecl : public ObjCImplDecl {
/// true of class extension has at least one bitfield ivar.
bool HasSynthBitfield : 1;

ObjCImplementationDecl(DeclContext *DC, SourceLocation L,
ObjCImplementationDecl(DeclContext *DC,
ObjCInterfaceDecl *classInterface,
ObjCInterfaceDecl *superDecl)
: ObjCImplDecl(ObjCImplementation, DC, L, classInterface),
ObjCInterfaceDecl *superDecl,
SourceLocation nameLoc, SourceLocation atStartLoc)
: ObjCImplDecl(ObjCImplementation, DC, classInterface, nameLoc, atStartLoc),
SuperClass(superDecl), IvarInitializers(0), NumIvarInitializers(0),
HasCXXStructors(false), HasSynthBitfield(false) {}
public:
static ObjCImplementationDecl *Create(ASTContext &C, DeclContext *DC,
SourceLocation L,
ObjCInterfaceDecl *classInterface,
ObjCInterfaceDecl *superDecl);
ObjCInterfaceDecl *superDecl,
SourceLocation nameLoc,
SourceLocation atStartLoc);

/// init_iterator - Iterates through the ivar initializer list.
typedef CXXCtorInitializer **init_iterator;
Expand Down
22 changes: 12 additions & 10 deletions lib/AST/ASTImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2983,7 +2983,7 @@ Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
ObjCCategoryDecl *ToCategory = MergeWithCategory;
if (!ToCategory) {
ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Importer.Import(D->getAtLoc()),
Importer.Import(D->getAtStartLoc()),
Loc,
Importer.Import(D->getCategoryNameLoc()),
Name.getAsIdentifierInfo(),
Expand Down Expand Up @@ -3056,8 +3056,9 @@ Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
ObjCProtocolDecl *ToProto = MergeWithProtocol;
if (!ToProto || ToProto->isForwardDecl()) {
if (!ToProto) {
ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
Name.getAsIdentifierInfo());
ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
Name.getAsIdentifierInfo(), Loc,
Importer.Import(D->getAtStartLoc()));
ToProto->setForwardDecl(D->isForwardDecl());
ToProto->setLexicalDeclContext(LexicalDC);
LexicalDC->addDecl(ToProto);
Expand Down Expand Up @@ -3116,10 +3117,9 @@ Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
ObjCInterfaceDecl *ToIface = MergeWithIface;
if (!ToIface || ToIface->isForwardDecl()) {
if (!ToIface) {
ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
DC, Loc,
Name.getAsIdentifierInfo(),
Importer.Import(D->getClassLoc()),
ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
Importer.Import(D->getAtStartLoc()),
Name.getAsIdentifierInfo(), Loc,
D->isForwardDecl(),
D->isImplicitInterfaceDecl());
ToIface->setForwardDecl(D->isForwardDecl());
Expand Down Expand Up @@ -3229,9 +3229,10 @@ Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
return 0;

ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Importer.Import(D->getLocation()),
Importer.Import(D->getIdentifier()),
Category->getClassInterface());
Category->getClassInterface(),
Importer.Import(D->getLocation()),
Importer.Import(D->getAtStartLoc()));

DeclContext *LexicalDC = DC;
if (D->getDeclContext() != D->getLexicalDeclContext()) {
Expand Down Expand Up @@ -3273,8 +3274,9 @@ Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
// now.
Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
Importer.ImportContext(D->getDeclContext()),
Iface, Super,
Importer.Import(D->getLocation()),
Iface, Super);
Importer.Import(D->getAtStartLoc()));

if (D->getDeclContext() != D->getLexicalDeclContext()) {
DeclContext *LexicalDC
Expand Down
29 changes: 17 additions & 12 deletions lib/AST/DeclObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -603,11 +603,10 @@ ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
ObjCInterfaceDecl::
ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
SourceLocation CLoc, bool FD, bool isInternal)
: ObjCContainerDecl(ObjCInterface, DC, atLoc, Id),
: ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
TypeForDecl(0), SuperClass(0),
CategoryList(0), IvarList(0),
ForwardDecl(FD), InternalInterface(isInternal), ExternallyCompleted(false),
ClassLoc(CLoc) {
ForwardDecl(FD), InternalInterface(isInternal), ExternallyCompleted(false) {
}

void ObjCInterfaceDecl::LoadExternalDefinition() const {
Expand Down Expand Up @@ -847,9 +846,10 @@ ObjCAtDefsFieldDecl
//===----------------------------------------------------------------------===//

ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
SourceLocation L,
IdentifierInfo *Id) {
return new (C) ObjCProtocolDecl(DC, L, Id);
IdentifierInfo *Id,
SourceLocation nameLoc,
SourceLocation atStartLoc) {
return new (C) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc);
}

ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
Expand Down Expand Up @@ -979,9 +979,12 @@ void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {

ObjCCategoryImplDecl *
ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
SourceLocation L,IdentifierInfo *Id,
ObjCInterfaceDecl *ClassInterface) {
return new (C) ObjCCategoryImplDecl(DC, L, Id, ClassInterface);
IdentifierInfo *Id,
ObjCInterfaceDecl *ClassInterface,
SourceLocation nameLoc,
SourceLocation atStartLoc) {
return new (C) ObjCCategoryImplDecl(DC, Id, ClassInterface,
nameLoc, atStartLoc);
}

ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Expand Down Expand Up @@ -1056,10 +1059,12 @@ raw_ostream &clang::operator<<(raw_ostream &OS,

ObjCImplementationDecl *
ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
SourceLocation L,
ObjCInterfaceDecl *ClassInterface,
ObjCInterfaceDecl *SuperDecl) {
return new (C) ObjCImplementationDecl(DC, L, ClassInterface, SuperDecl);
ObjCInterfaceDecl *SuperDecl,
SourceLocation nameLoc,
SourceLocation atStartLoc) {
return new (C) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
nameLoc, atStartLoc);
}

void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
Expand Down
2 changes: 1 addition & 1 deletion lib/Rewrite/RewriteObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3359,7 +3359,7 @@ void RewriteObjC::SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl,
// This clause is segregated to avoid breaking the common case.
if (BufferContainsPPDirectives(startBuf, cursor)) {
SourceLocation L = RCDecl ? CDecl->getSuperClassLoc() :
CDecl->getClassLoc();
CDecl->getAtStartLoc();
const char *endHeader = SM->getCharacterData(L);
endHeader += Lexer::MeasureTokenLength(L, *SM, LangOpts);

Expand Down
20 changes: 10 additions & 10 deletions lib/Sema/SemaDeclObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,9 @@ ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
// FIXME: don't leak the objects passed in!
return IDecl;
} else {
IDecl->setLocation(AtInterfaceLoc);
IDecl->setLocation(ClassLoc);
IDecl->setForwardDecl(false);
IDecl->setClassLoc(ClassLoc);
IDecl->setAtStartLoc(AtInterfaceLoc);
// If the forward decl was in a PCH, we need to write it again in a
// dependent AST file.
IDecl->setChangedSinceDeserialization(true);
Expand Down Expand Up @@ -595,8 +595,8 @@ Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
// Repeat in dependent AST files.
PDecl->setChangedSinceDeserialization(true);
} else {
PDecl = ObjCProtocolDecl::Create(Context, CurContext,
AtProtoInterfaceLoc,ProtocolName);
PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
ProtocolLoc, AtProtoInterfaceLoc);
PushOnScopeChains(PDecl, TUScope);
PDecl->setForwardDecl(false);
}
Expand Down Expand Up @@ -696,8 +696,8 @@ Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second);
bool isNew = false;
if (PDecl == 0) { // Not already seen?
PDecl = ObjCProtocolDecl::Create(Context, CurContext,
IdentList[i].second, Ident);
PDecl = ObjCProtocolDecl::Create(Context, CurContext, Ident,
IdentList[i].second, AtProtocolLoc);
PushOnScopeChains(PDecl, TUScope, false);
isNew = true;
}
Expand Down Expand Up @@ -806,8 +806,8 @@ Decl *Sema::ActOnStartCategoryImplementation(
}

ObjCCategoryImplDecl *CDecl =
ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
IDecl);
ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
ClassLoc, AtCatImplLoc);
/// Check that class of this category is already completely declared.
if (!IDecl || IDecl->isForwardDecl()) {
Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Expand Down Expand Up @@ -925,8 +925,8 @@ Decl *Sema::ActOnStartClassImplementation(
}

ObjCImplementationDecl* IMPDecl =
ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
IDecl, SDecl);
ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
ClassLoc, AtClassImplLoc);

if (CheckObjCDeclScope(IMPDecl))
return IMPDecl;
Expand Down
Loading

0 comments on commit 1711fc9

Please sign in to comment.