objective-c: Treat top-level objective-c declarations
, such as list of forward @class decls, in a DeclGroup
node. Deal with its consequence throught clang. This
is in preparation for more Sema work ahead. // rdar://8843851.
Feel free to reverse if it breaks something important
and I am unavailable.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@138709 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Rewrite/RewriteObjC.cpp b/lib/Rewrite/RewriteObjC.cpp
index 3ba7e90..ba51806 100644
--- a/lib/Rewrite/RewriteObjC.cpp
+++ b/lib/Rewrite/RewriteObjC.cpp
@@ -161,8 +161,13 @@
// Top Level Driver code.
virtual void HandleTopLevelDecl(DeclGroupRef D) {
- for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
+ for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) {
+ if (isa<ObjCClassDecl>((*I))) {
+ RewriteForwardClassDecl(D);
+ break;
+ }
HandleTopLevelSingleDecl(*I);
+ }
}
void HandleTopLevelSingleDecl(Decl *D);
void HandleDeclInMainFile(Decl *D);
@@ -241,7 +246,7 @@
// Syntactic Rewriting.
void RewriteInclude();
- void RewriteForwardClassDecl(ObjCClassDecl *Dcl);
+ void RewriteForwardClassDecl(DeclGroupRef D);
void RewritePropertyImplDecl(ObjCPropertyImplDecl *PID,
ObjCImplementationDecl *IMD,
ObjCCategoryImplDecl *CID);
@@ -886,30 +891,28 @@
InsertText(onePastSemiLoc, Setr);
}
-void RewriteObjC::RewriteForwardClassDecl(ObjCClassDecl *ClassDecl) {
- // Get the start location and compute the semi location.
- SourceLocation startLoc = ClassDecl->getLocation();
- const char *startBuf = SM->getCharacterData(startLoc);
- const char *semiPtr = strchr(startBuf, ';');
-
- // Translate to typedef's that forward reference structs with the same name
- // as the class. As a convenience, we include the original declaration
- // as a comment.
+void RewriteObjC::RewriteForwardClassDecl(DeclGroupRef D) {
+ SourceLocation startLoc;
std::string typedefString;
- typedefString += "// @class ";
- for (ObjCClassDecl::iterator I = ClassDecl->begin(), E = ClassDecl->end();
- I != E; ++I) {
- ObjCInterfaceDecl *ForwardDecl = I->getInterface();
- typedefString += ForwardDecl->getNameAsString();
- if (I+1 != E)
- typedefString += ", ";
- else
+ const char *startBuf = 0;
+ const char *semiPtr = 0;
+ for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) {
+ ObjCClassDecl *ClassDecl = cast<ObjCClassDecl>(*I);
+ ObjCInterfaceDecl *ForwardDecl = ClassDecl->getForwardInterfaceDecl();
+ if (I == D.begin()) {
+ // Get the start location and compute the semi location.
+ startLoc = ClassDecl->getLocation();
+ startBuf = SM->getCharacterData(startLoc);
+ semiPtr = strchr(startBuf, ';');
+ typedefString += "// @class ";
+ typedefString += ForwardDecl->getNameAsString();
typedefString += ";\n";
- }
+ }
+ // Translate to typedef's that forward reference structs with the same name
+ // as the class. As a convenience, we include the original declaration
+ // as a comment.
- for (ObjCClassDecl::iterator I = ClassDecl->begin(), E = ClassDecl->end();
- I != E; ++I) {
- ObjCInterfaceDecl *ForwardDecl = I->getInterface();
+
typedefString += "#ifndef _REWRITER_typedef_";
typedefString += ForwardDecl->getNameAsString();
typedefString += "\n";
@@ -5887,8 +5890,8 @@
ClassImplementation.push_back(CI);
else if (ObjCCategoryImplDecl *CI = dyn_cast<ObjCCategoryImplDecl>(D))
CategoryImplementation.push_back(CI);
- else if (ObjCClassDecl *CD = dyn_cast<ObjCClassDecl>(D))
- RewriteForwardClassDecl(CD);
+ else if (isa<ObjCClassDecl>(D))
+ assert(false && "RewriteObjC::HandleDeclInMainFile - ObjCClassDecl");
else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
RewriteObjCQualifiedInterfaceTypes(VD);
if (isTopLevelBlockPointerType(VD->getType()))