From: Chris Lattner Date: Thu, 25 Oct 2007 17:07:24 +0000 (+0000) Subject: Convert one type of metadata to use std::string instead of X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=158ecb9767faf87c2a33df3baec1b160dcc0be84;p=clang Convert one type of metadata to use std::string instead of printf as an example. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@43346 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/Driver/RewriteTest.cpp b/Driver/RewriteTest.cpp index 17fd0473fc..afe9493068 100644 --- a/Driver/RewriteTest.cpp +++ b/Driver/RewriteTest.cpp @@ -17,7 +17,9 @@ #include "clang/AST/ASTConsumer.h" #include "clang/Basic/SourceManager.h" #include "clang/Basic/IdentifierTable.h" +#include "llvm/ADT/StringExtras.h" using namespace clang; +using llvm::utostr; namespace { class RewriteTest : public ASTConsumer { @@ -70,7 +72,8 @@ namespace { int NumMethods, bool IsInstanceMethod, const char *prefix, - const char *ClassName); + const char *ClassName, + std::string &Result); void RewriteObjcProtocolsMetaData(ObjcProtocolDecl **Protocols, int NumProtocols, @@ -360,7 +363,8 @@ void RewriteTest::RewriteObjcMethodsMetaData(ObjcMethodDecl **Methods, int NumMethods, bool IsInstanceMethod, const char *prefix, - const char *ClassName) { + const char *ClassName, + std::string &Result) { static bool objc_impl_method = false; if (NumMethods > 0 && !objc_impl_method) { /* struct _objc_method { @@ -369,11 +373,11 @@ void RewriteTest::RewriteObjcMethodsMetaData(ObjcMethodDecl **Methods, void *_imp; } */ - printf("\nstruct _objc_method {\n"); - printf("\tSEL _cmd;\n"); - printf("\tchar *method_types;\n"); - printf("\tvoid *_imp;\n"); - printf("};\n"); + Result += "\nstruct _objc_method {\n"; + Result += "\tSEL _cmd;\n"; + Result += "\tchar *method_types;\n"; + Result += "\tvoid *_imp;\n"; + Result += "};\n"; objc_impl_method = true; } // Build _objc_method_list for class's methods if needed @@ -384,21 +388,27 @@ void RewriteTest::RewriteObjcMethodsMetaData(ObjcMethodDecl **Methods, struct _objc_method method_list[method_count]; } */ - printf("\nstatic struct {\n"); - printf("\tstruct _objc_method_list *next_method;\n"); - printf("\tint method_count;\n"); - printf("\tstruct _objc_method method_list[%d];\n", NumMethods); - printf("} _OBJC_%s%s_METHODS_%s " - "__attribute__ ((section (\"__OBJC, __%s_meth\")))= " - "{\n\t0, %d\n", prefix, IsInstanceMethod ? "INSTANCE" : "CLASS", - ClassName, IsInstanceMethod ? "inst" : "cls", NumMethods); - for (int i = 0; i < NumMethods; i++) + Result += "\nstatic struct {\n"; + Result += "\tstruct _objc_method_list *next_method;\n"; + Result += "\tint method_count;\n"; + Result += "\tstruct _objc_method method_list[" + utostr(NumMethods) +"];\n"; + Result += "} _OBJC_"; + Result += prefix; + Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; + Result += "_METHODS_"; + Result += ClassName; + Result += " __attribute__ ((section (\"__OBJC, __"; + Result += IsInstanceMethod ? "inst" : "cls"; + Result += "_meth\")))= {\n\t0, " + utostr(NumMethods) + "\n"; + + for (int i = 0; i < NumMethods; i++) { // TODO: 1) method selector name may hav to go into their own section // 2) encode method types for use here (which may have to go into // __meth_var_types section, 3) Need method address as 3rd initializer. - printf("\t,(SEL)\"%s\", \"\", 0\n", - Methods[i]->getSelector().getName().c_str()); - printf("};\n"); + Result += "\t,(SEL)\"" + Methods[i]->getSelector().getName() + + "\", \"\", 0\n"; + } + Result += "};\n"; } } @@ -545,17 +555,23 @@ void RewriteTest::RewriteObjcCategoryImplDecl(ObjcCategoryImplDecl *IDecl) { strlen(ClassDecl->getName()) + strlen(IDecl->getName()) + 2); sprintf(FullCategoryName, "%s_%s", ClassDecl->getName(), IDecl->getName()); + std::string ResultStr; + // Build _objc_method_list for class's instance methods if needed RewriteObjcMethodsMetaData(IDecl->getInstanceMethods(), IDecl->getNumInstanceMethods(), true, - "CATEGORY_", FullCategoryName); + "CATEGORY_", FullCategoryName, ResultStr); // Build _objc_method_list for class's class methods if needed RewriteObjcMethodsMetaData(IDecl->getClassMethods(), IDecl->getNumClassMethods(), false, - "CATEGORY_", FullCategoryName); + "CATEGORY_", FullCategoryName, ResultStr); + + // For now just print the string out. It should be passed to the other + // functions to collect all metadata info into the string. + printf("%s", ResultStr.c_str()); // Protocols referenced in class declaration? RewriteObjcProtocolsMetaData(CDecl->getReferencedProtocols(), @@ -666,18 +682,22 @@ void RewriteTest::RewriteObjcClassMetaData(ObjcImplementationDecl *IDecl) { printf("};\n"); } + std::string ResultStr; + // Build _objc_method_list for class's instance methods if needed RewriteObjcMethodsMetaData(IDecl->getInstanceMethods(), IDecl->getNumInstanceMethods(), - true, - "", IDecl->getName()); + true, "", IDecl->getName(), ResultStr); // Build _objc_method_list for class's class methods if needed RewriteObjcMethodsMetaData(IDecl->getClassMethods(), IDecl->getNumClassMethods(), - false, - "", IDecl->getName()); - + false, "", IDecl->getName(), ResultStr); + + // For now just print the string out. It should be passed to the other + // functions to collect all metadata info into the string. + printf("%s", ResultStr.c_str()); + // Protocols referenced in class declaration? RewriteObjcProtocolsMetaData(CDecl->getReferencedProtocols(), CDecl->getNumIntfRefProtocols(), diff --git a/clang.xcodeproj/project.pbxproj b/clang.xcodeproj/project.pbxproj index 3980669069..0b0d2d4bf7 100644 --- a/clang.xcodeproj/project.pbxproj +++ b/clang.xcodeproj/project.pbxproj @@ -753,7 +753,6 @@ 08FB7793FE84155DC02AAC07 /* Project object */ = { isa = PBXProject; buildConfigurationList = 1DEB923508733DC60010E9CD /* Build configuration list for PBXProject "clang" */; - compatibilityVersion = "Xcode 2.4"; hasScannedForEncodings = 1; mainGroup = 08FB7794FE84155DC02AAC07 /* clang */; projectDirPath = "";