[PATCH] D39903: [libclang] Allow pretty printing declarations
nik added a comment. Ping. Repository: rC Clang https://reviews.llvm.org/D39903 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D39903: [libclang] Allow pretty printing declarations
nik added a comment. Ping... Repository: rC Clang https://reviews.llvm.org/D39903 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D39903: [libclang] Allow pretty printing declarations
nik updated this revision to Diff 129258. nik added a comment. Rebased only, please review. Repository: rC Clang https://reviews.llvm.org/D39903 Files: include/clang-c/Index.h test/Index/print-display-names.cpp tools/c-index-test/c-index-test.c tools/libclang/CIndex.cpp tools/libclang/libclang.exports Index: tools/libclang/libclang.exports === --- tools/libclang/libclang.exports +++ tools/libclang/libclang.exports @@ -178,6 +178,8 @@ clang_getCursorCompletionString clang_getCursorDefinition clang_getCursorDisplayName +clang_getCursorPrintingPolicy +clang_getCursorPrettyPrinted clang_getCursorExtent clang_getCursorExceptionSpecificationType clang_getCursorKind @@ -359,3 +361,56 @@ clang_EvalResult_getAsDouble clang_EvalResult_getAsStr clang_EvalResult_dispose +clang_PrintingPolicy_dispose +clang_PrintingPolicy_getIndentation +clang_PrintingPolicy_getSuppressSpecifiers +clang_PrintingPolicy_getSuppressTagKeyword +clang_PrintingPolicy_getIncludeTagDefinition +clang_PrintingPolicy_getSuppressScope +clang_PrintingPolicy_getSuppressUnwrittenScope +clang_PrintingPolicy_getSuppressInitializers +clang_PrintingPolicy_getConstantArraySizeAsWritten +clang_PrintingPolicy_getAnonymousTagLocations +clang_PrintingPolicy_getSuppressStrongLifetime +clang_PrintingPolicy_getSuppressLifetimeQualifiers +clang_PrintingPolicy_getSuppressTemplateArgsInCXXConstructors +clang_PrintingPolicy_getBool +clang_PrintingPolicy_getRestrict +clang_PrintingPolicy_getAlignof +clang_PrintingPolicy_getUnderscoreAlignof +clang_PrintingPolicy_getUseVoidForZeroParams +clang_PrintingPolicy_getTerseOutput +clang_PrintingPolicy_getPolishForDeclaration +clang_PrintingPolicy_getHalf +clang_PrintingPolicy_getMSWChar +clang_PrintingPolicy_getIncludeNewlines +clang_PrintingPolicy_getMSVCFormatting +clang_PrintingPolicy_getConstantsAsWritten +clang_PrintingPolicy_getSuppressImplicitBase +clang_PrintingPolicy_getFullyQualifiedName +clang_PrintingPolicy_setIndentation +clang_PrintingPolicy_setSuppressSpecifiers +clang_PrintingPolicy_setSuppressTagKeyword +clang_PrintingPolicy_setIncludeTagDefinition +clang_PrintingPolicy_setSuppressScope +clang_PrintingPolicy_setSuppressUnwrittenScope +clang_PrintingPolicy_setSuppressInitializers +clang_PrintingPolicy_setConstantArraySizeAsWritten +clang_PrintingPolicy_setAnonymousTagLocations +clang_PrintingPolicy_setSuppressStrongLifetime +clang_PrintingPolicy_setSuppressLifetimeQualifiers +clang_PrintingPolicy_setSuppressTemplateArgsInCXXConstructors +clang_PrintingPolicy_setBool +clang_PrintingPolicy_setRestrict +clang_PrintingPolicy_setAlignof +clang_PrintingPolicy_setUnderscoreAlignof +clang_PrintingPolicy_setUseVoidForZeroParams +clang_PrintingPolicy_setTerseOutput +clang_PrintingPolicy_setPolishForDeclaration +clang_PrintingPolicy_setHalf +clang_PrintingPolicy_setMSWChar +clang_PrintingPolicy_setIncludeNewlines +clang_PrintingPolicy_setMSVCFormatting +clang_PrintingPolicy_setConstantsAsWritten +clang_PrintingPolicy_setSuppressImplicitBase +clang_PrintingPolicy_setFullyQualifiedName Index: tools/libclang/CIndex.cpp === --- tools/libclang/CIndex.cpp +++ tools/libclang/CIndex.cpp @@ -4706,6 +4706,107 @@ return cxstring::createSet(Manglings); } +CXPrintingPolicy clang_getCursorPrintingPolicy(CXCursor C) { + if (clang_Cursor_isNull(C)) +return 0; + return new PrintingPolicy(getCursorContext(C).getPrintingPolicy()); +} + +void clang_PrintingPolicy_dispose(CXPrintingPolicy policy) { + if (policy) +delete static_cast(policy); +} + +#define DEFINE_PRINTING_POLICY_GETTER(name)\ + unsigned clang_PrintingPolicy_get##name(CXPrintingPolicy policy) { \ +if (!policy) \ + return 0;\ +return static_cast(policy)->name;\ + } +DEFINE_PRINTING_POLICY_GETTER(Indentation) +DEFINE_PRINTING_POLICY_GETTER(SuppressSpecifiers) +DEFINE_PRINTING_POLICY_GETTER(SuppressTagKeyword) +DEFINE_PRINTING_POLICY_GETTER(IncludeTagDefinition) +DEFINE_PRINTING_POLICY_GETTER(SuppressScope) +DEFINE_PRINTING_POLICY_GETTER(SuppressUnwrittenScope) +DEFINE_PRINTING_POLICY_GETTER(SuppressInitializers) +DEFINE_PRINTING_POLICY_GETTER(ConstantArraySizeAsWritten) +DEFINE_PRINTING_POLICY_GETTER(AnonymousTagLocations) +DEFINE_PRINTING_POLICY_GETTER(SuppressStrongLifetime) +DEFINE_PRINTING_POLICY_GETTER(SuppressLifetimeQualifiers) +DEFINE_PRINTING_POLICY_GETTER(SuppressTemplateArgsInCXXConstructors) +DEFINE_PRINTING_POLICY_GETTER(Bool) +DEFINE_PRINTING_POLICY_GETTER(Restrict) +DEFINE_PRINTING_POLICY_GETTER(Alignof) +DEFINE_PRINTING_POLICY_GETTER(UnderscoreAlignof) +DEFINE_PRINTING_POLICY_GETTER(UseVoidForZeroParams) +DEFINE_PRINTING_POLICY_GETTER(TerseOutput) +DEFINE_PRINTING_POLICY_GETTER(PolishFor
[PATCH] D39903: [libclang] Allow pretty printing declarations
jbcoe added inline comments. Comment at: include/clang-c/Index.h:4118 + */ +CINDEX_LINKAGE unsigned clang_PrintingPolicy_getIndentation(CXPrintingPolicy); + Could one use an enum to get/set different properties of the policy? ``` clang_PrintingPolicy_get(CXPrintingPolicy Policy, unsigned Property); clang_PrintingPolicy_set(CXPrintingPolicy Policy, unsigned Property, unsigned Value); ``` I've seen other C-API's (for Linear and Quadratic programming) follow a similar approach quite extensibly. It would significantly reduce the size of the API. Repository: rC Clang https://reviews.llvm.org/D39903 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D39903: [libclang] Allow pretty printing declarations
nik updated this revision to Diff 129426. nik added a comment. > Could one use an enum to get/set different properties of the policy? > > I've seen other C-API's (for Linear and Quadratic programming) follow a > similar approach quite extensibly. > > It would significantly reduce the size of the API. You are right and rethinking this, I see no problem with it. I thought of that too (see my very first comment to this change), but then for some reason the PrintingPolicy::Indentation (unsigned : 8) has driven me to this version. I probably have only thought of strictly false/true values to set/get. OK, adapted to enum version. Repository: rC Clang https://reviews.llvm.org/D39903 Files: include/clang-c/Index.h test/Index/print-display-names.cpp tools/c-index-test/c-index-test.c tools/libclang/CIndex.cpp tools/libclang/libclang.exports Index: tools/libclang/libclang.exports === --- tools/libclang/libclang.exports +++ tools/libclang/libclang.exports @@ -178,6 +178,8 @@ clang_getCursorCompletionString clang_getCursorDefinition clang_getCursorDisplayName +clang_getCursorPrintingPolicy +clang_getCursorPrettyPrinted clang_getCursorExtent clang_getCursorExceptionSpecificationType clang_getCursorKind @@ -359,3 +361,6 @@ clang_EvalResult_getAsDouble clang_EvalResult_getAsStr clang_EvalResult_dispose +clang_PrintingPolicy_get +clang_PrintingPolicy_set +clang_PrintingPolicy_dispose Index: tools/libclang/CIndex.cpp === --- tools/libclang/CIndex.cpp +++ tools/libclang/CIndex.cpp @@ -4706,6 +4706,191 @@ return cxstring::createSet(Manglings); } +CXPrintingPolicy clang_getCursorPrintingPolicy(CXCursor C) { + if (clang_Cursor_isNull(C)) +return 0; + return new PrintingPolicy(getCursorContext(C).getPrintingPolicy()); +} + +void clang_PrintingPolicy_dispose(CXPrintingPolicy Policy) { + if (Policy) +delete static_cast(Policy); +} + +unsigned clang_PrintingPolicy_get(CXPrintingPolicy Policy, + enum CXPrintingPolicyProperty Property) { + if (!Policy) +return 0; + + PrintingPolicy *P = static_cast(Policy); + switch (Property) { + case CXPrintingPolicy_Indentation: +return P->Indentation; + case CXPrintingPolicy_SuppressSpecifiers: +return P->SuppressSpecifiers; + case CXPrintingPolicy_SuppressTagKeyword: +return P->SuppressTagKeyword; + case CXPrintingPolicy_IncludeTagDefinition: +return P->IncludeTagDefinition; + case CXPrintingPolicy_SuppressScope: +return P->SuppressScope; + case CXPrintingPolicy_SuppressUnwrittenScope: +return P->SuppressUnwrittenScope; + case CXPrintingPolicy_SuppressInitializers: +return P->SuppressInitializers; + case CXPrintingPolicy_ConstantArraySizeAsWritten: +return P->ConstantArraySizeAsWritten; + case CXPrintingPolicy_AnonymousTagLocations: +return P->AnonymousTagLocations; + case CXPrintingPolicy_SuppressStrongLifetime: +return P->SuppressStrongLifetime; + case CXPrintingPolicy_SuppressLifetimeQualifiers: +return P->SuppressLifetimeQualifiers; + case CXPrintingPolicy_SuppressTemplateArgsInCXXConstructors: +return P->SuppressTemplateArgsInCXXConstructors; + case CXPrintingPolicy_Bool: +return P->Bool; + case CXPrintingPolicy_Restrict: +return P->Restrict; + case CXPrintingPolicy_Alignof: +return P->Alignof; + case CXPrintingPolicy_UnderscoreAlignof: +return P->UnderscoreAlignof; + case CXPrintingPolicy_UseVoidForZeroParams: +return P->UseVoidForZeroParams; + case CXPrintingPolicy_TerseOutput: +return P->TerseOutput; + case CXPrintingPolicy_PolishForDeclaration: +return P->PolishForDeclaration; + case CXPrintingPolicy_Half: +return P->Half; + case CXPrintingPolicy_MSWChar: +return P->MSWChar; + case CXPrintingPolicy_IncludeNewlines: +return P->IncludeNewlines; + case CXPrintingPolicy_MSVCFormatting: +return P->MSVCFormatting; + case CXPrintingPolicy_ConstantsAsWritten: +return P->ConstantsAsWritten; + case CXPrintingPolicy_SuppressImplicitBase: +return P->SuppressImplicitBase; + case CXPrintingPolicy_FullyQualifiedName: +return P->FullyQualifiedName; + } + + return 0; +} + +void clang_PrintingPolicy_set(CXPrintingPolicy Policy, + enum CXPrintingPolicyProperty Property, + unsigned Value) { + if (!Policy) +return; + + PrintingPolicy *P = static_cast(Policy); + switch (Property) { + case CXPrintingPolicy_Indentation: +P->Indentation = Value; +break; + case CXPrintingPolicy_SuppressSpecifiers: +P->SuppressSpecifiers = Value; +break; + case CXPrintingPolicy_SuppressTagKeyword: +P->SuppressTagKeyword = Value; +break; + case CXPrintingPolicy_IncludeTagDefinition: +P->IncludeTagDefinition = Value; +break; + case CXPrintingPolicy_SuppressScope: +P->SuppressScope =
[PATCH] D39903: [libclang] Allow pretty printing declarations
nik updated this revision to Diff 129427. nik added a comment. Used macros as in a previous version to make it less verbose and error prone. Repository: rC Clang https://reviews.llvm.org/D39903 Files: include/clang-c/Index.h test/Index/print-display-names.cpp tools/c-index-test/c-index-test.c tools/libclang/CIndex.cpp tools/libclang/libclang.exports Index: tools/libclang/libclang.exports === --- tools/libclang/libclang.exports +++ tools/libclang/libclang.exports @@ -178,6 +178,8 @@ clang_getCursorCompletionString clang_getCursorDefinition clang_getCursorDisplayName +clang_getCursorPrintingPolicy +clang_getCursorPrettyPrinted clang_getCursorExtent clang_getCursorExceptionSpecificationType clang_getCursorKind @@ -359,3 +361,6 @@ clang_EvalResult_getAsDouble clang_EvalResult_getAsStr clang_EvalResult_dispose +clang_PrintingPolicy_get +clang_PrintingPolicy_set +clang_PrintingPolicy_dispose Index: tools/libclang/CIndex.cpp === --- tools/libclang/CIndex.cpp +++ tools/libclang/CIndex.cpp @@ -4706,6 +4706,125 @@ return cxstring::createSet(Manglings); } +CXPrintingPolicy clang_getCursorPrintingPolicy(CXCursor C) { + if (clang_Cursor_isNull(C)) +return 0; + return new PrintingPolicy(getCursorContext(C).getPrintingPolicy()); +} + +void clang_PrintingPolicy_dispose(CXPrintingPolicy Policy) { + if (Policy) +delete static_cast(Policy); +} + +#define HANDLE_CASE(P, PROPERTY_NAME) \ + case CXPrintingPolicy_##PROPERTY_NAME: \ +return P->PROPERTY_NAME; + +unsigned clang_PrintingPolicy_get(CXPrintingPolicy Policy, + enum CXPrintingPolicyProperty Property) { + if (!Policy) +return 0; + + PrintingPolicy *P = static_cast(Policy); + switch (Property) { +HANDLE_CASE(P, Indentation) +HANDLE_CASE(P, SuppressSpecifiers) +HANDLE_CASE(P, SuppressTagKeyword) +HANDLE_CASE(P, IncludeTagDefinition) +HANDLE_CASE(P, SuppressScope) +HANDLE_CASE(P, SuppressUnwrittenScope) +HANDLE_CASE(P, SuppressInitializers) +HANDLE_CASE(P, ConstantArraySizeAsWritten) +HANDLE_CASE(P, AnonymousTagLocations) +HANDLE_CASE(P, SuppressStrongLifetime) +HANDLE_CASE(P, SuppressLifetimeQualifiers) +HANDLE_CASE(P, SuppressTemplateArgsInCXXConstructors) +HANDLE_CASE(P, Bool) +HANDLE_CASE(P, Restrict) +HANDLE_CASE(P, Alignof) +HANDLE_CASE(P, UnderscoreAlignof) +HANDLE_CASE(P, UseVoidForZeroParams) +HANDLE_CASE(P, TerseOutput) +HANDLE_CASE(P, PolishForDeclaration) +HANDLE_CASE(P, Half) +HANDLE_CASE(P, MSWChar) +HANDLE_CASE(P, IncludeNewlines) +HANDLE_CASE(P, MSVCFormatting) +HANDLE_CASE(P, ConstantsAsWritten) +HANDLE_CASE(P, SuppressImplicitBase) +HANDLE_CASE(P, FullyQualifiedName) + } + + return 0; +} + +#undef HANDLE_CASE +#define HANDLE_CASE(P, PROPERTY_NAME, VALUE) \ + case CXPrintingPolicy_##PROPERTY_NAME: \ +P->PROPERTY_NAME = VALUE; \ +break; + +void clang_PrintingPolicy_set(CXPrintingPolicy Policy, + enum CXPrintingPolicyProperty Property, + unsigned Value) { + if (!Policy) +return; + + PrintingPolicy *P = static_cast(Policy); + switch (Property) { +HANDLE_CASE(P, Indentation, Value) +HANDLE_CASE(P, SuppressSpecifiers, Value) +HANDLE_CASE(P, SuppressTagKeyword, Value) +HANDLE_CASE(P, IncludeTagDefinition, Value) +HANDLE_CASE(P, SuppressScope, Value) +HANDLE_CASE(P, SuppressUnwrittenScope, Value) +HANDLE_CASE(P, SuppressInitializers, Value) +HANDLE_CASE(P, ConstantArraySizeAsWritten, Value) +HANDLE_CASE(P, AnonymousTagLocations, Value) +HANDLE_CASE(P, SuppressStrongLifetime, Value) +HANDLE_CASE(P, SuppressLifetimeQualifiers, Value) +HANDLE_CASE(P, SuppressTemplateArgsInCXXConstructors, Value) +HANDLE_CASE(P, Bool, Value) +HANDLE_CASE(P, Restrict, Value) +HANDLE_CASE(P, Alignof, Value) +HANDLE_CASE(P, UnderscoreAlignof, Value) +HANDLE_CASE(P, UseVoidForZeroParams, Value) +HANDLE_CASE(P, TerseOutput, Value) +HANDLE_CASE(P, PolishForDeclaration, Value) +HANDLE_CASE(P, Half, Value) +HANDLE_CASE(P, MSWChar, Value) +HANDLE_CASE(P, IncludeNewlines, Value) +HANDLE_CASE(P, MSVCFormatting, Value) +HANDLE_CASE(P, ConstantsAsWritten, Value) +HANDLE_CASE(P, SuppressImplicitBase, Value) +HANDLE_CASE(P, FullyQualifiedName, Value) + } +} + +#undef HANDLE_CASE + +CXString clang_getCursorPrettyPrinted(CXCursor C, CXPrintingPolicy cxPolicy) { + if (clang_Cursor_isNull(C)) +return cxstring::createEmpty(); + + if (clang_isDeclaration(C.kind)) { +const Decl *D = getCursorDecl(C); +if
[PATCH] D39903: [libclang] Allow pretty printing declarations
jbcoe added a comment. It might be worth adding some very simple get/set tests to ensure that properties are set as intended. Comment at: tools/libclang/CIndex.cpp:4720 + +#define HANDLE_CASE(P, PROPERTY_NAME) \ + case CXPrintingPolicy_##PROPERTY_NAME: \ I’m not convinced that the code replaced by the macro is sufficiently complicated to justify use of a macro. Comment at: tools/libclang/CIndex.cpp:4763 +#undef HANDLE_CASE +#define HANDLE_CASE(P, PROPERTY_NAME, VALUE) \ + case CXPrintingPolicy_##PROPERTY_NAME: \ I’m not convinced that the code replaced by the macro is sufficiently complicated to justify use of a macro. Repository: rC Clang https://reviews.llvm.org/D39903 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D39903: [libclang] Allow pretty printing declarations
jbcoe added inline comments. Comment at: tools/libclang/libclang.exports:365 +clang_PrintingPolicy_get +clang_PrintingPolicy_set +clang_PrintingPolicy_dispose clang_PrintingPolicy_setProperty and clang_PrintingPolicy_getProperty might be better names (I know the ones you have used were my earlier suggestions). Repository: rC Clang https://reviews.llvm.org/D39903 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D39903: [libclang] Allow pretty printing declarations
nik added a comment. > It might be worth adding some very simple get/set tests to ensure that > properties are set as intended. clang_PrintingPolicy_setProperty is already called in c-index-test.c and covered with test/Index/print-display-names.cpp. Do you have another kind of test in mind? I'm not sure how to best test clang_PrintingPolicy_getProperty. I don't see how to get that cleanly covered within c-index-test.c, too. After calling the setter, one could call the getter and verify that it's the same. But c-index-test shouldn't do something like this. I've added a test in LibclangTest.cpp with which I'm not too happy (an extra parse just to test a getter feels wrong). Comment at: tools/libclang/CIndex.cpp:4720 + +#define HANDLE_CASE(P, PROPERTY_NAME) \ + case CXPrintingPolicy_##PROPERTY_NAME: \ jbcoe wrote: > I’m not convinced that the code replaced by the macro is sufficiently > complicated to justify use of a macro. I find it easier to read and verify (mapping to the wrong property can't happen), but I've no strong opinion here, fine with me - reverted to non-macro case. Comment at: tools/libclang/libclang.exports:365 +clang_PrintingPolicy_get +clang_PrintingPolicy_set +clang_PrintingPolicy_dispose jbcoe wrote: > clang_PrintingPolicy_setProperty and clang_PrintingPolicy_getProperty might > be better names (I know the ones you have used were my earlier suggestions). Agree! Repository: rC Clang https://reviews.llvm.org/D39903 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D39903: [libclang] Allow pretty printing declarations
nik updated this revision to Diff 129591. nik added a comment. Addressed comments. Repository: rC Clang https://reviews.llvm.org/D39903 Files: include/clang-c/Index.h test/Index/print-display-names.cpp tools/c-index-test/c-index-test.c tools/libclang/CIndex.cpp tools/libclang/libclang.exports unittests/libclang/LibclangTest.cpp Index: unittests/libclang/LibclangTest.cpp === --- unittests/libclang/LibclangTest.cpp +++ unittests/libclang/LibclangTest.cpp @@ -572,3 +572,26 @@ EXPECT_EQ(0U, clang_getNumDiagnostics(ClangTU)); DisplayDiagnostics(); } + +class LibclangPrintingPolicyTest : public LibclangParseTest { +public: + CXPrintingPolicy Policy = nullptr; + + void SetUp() override { +LibclangParseTest::SetUp(); +std::string File = "file.cpp"; +WriteFile(File, "int i;\n"); +ClangTU = clang_parseTranslationUnit(Index, File.c_str(), nullptr, 0, + nullptr, 0, TUFlags); +CXCursor TUCursor = clang_getTranslationUnitCursor(ClangTU); +Policy = clang_getCursorPrintingPolicy(TUCursor); + } + void TearDown() override { +clang_PrintingPolicy_dispose(Policy); +LibclangParseTest::TearDown(); + } +}; + +TEST_F(LibclangPrintingPolicyTest, GetProperty) { + EXPECT_EQ(2U, clang_PrintingPolicy_getProperty(Policy, CXPrintingPolicy_Indentation)); +} Index: tools/libclang/libclang.exports === --- tools/libclang/libclang.exports +++ tools/libclang/libclang.exports @@ -178,6 +178,8 @@ clang_getCursorCompletionString clang_getCursorDefinition clang_getCursorDisplayName +clang_getCursorPrintingPolicy +clang_getCursorPrettyPrinted clang_getCursorExtent clang_getCursorExceptionSpecificationType clang_getCursorKind @@ -359,3 +361,6 @@ clang_EvalResult_getAsDouble clang_EvalResult_getAsStr clang_EvalResult_dispose +clang_PrintingPolicy_getProperty +clang_PrintingPolicy_setProperty +clang_PrintingPolicy_dispose Index: tools/libclang/CIndex.cpp === --- tools/libclang/CIndex.cpp +++ tools/libclang/CIndex.cpp @@ -4706,6 +4706,192 @@ return cxstring::createSet(Manglings); } +CXPrintingPolicy clang_getCursorPrintingPolicy(CXCursor C) { + if (clang_Cursor_isNull(C)) +return 0; + return new PrintingPolicy(getCursorContext(C).getPrintingPolicy()); +} + +void clang_PrintingPolicy_dispose(CXPrintingPolicy Policy) { + if (Policy) +delete static_cast(Policy); +} + +unsigned +clang_PrintingPolicy_getProperty(CXPrintingPolicy Policy, + enum CXPrintingPolicyProperty Property) { + if (!Policy) +return 0; + + PrintingPolicy *P = static_cast(Policy); + switch (Property) { + case CXPrintingPolicy_Indentation: +return P->Indentation; + case CXPrintingPolicy_SuppressSpecifiers: +return P->SuppressSpecifiers; + case CXPrintingPolicy_SuppressTagKeyword: +return P->SuppressTagKeyword; + case CXPrintingPolicy_IncludeTagDefinition: +return P->IncludeTagDefinition; + case CXPrintingPolicy_SuppressScope: +return P->SuppressScope; + case CXPrintingPolicy_SuppressUnwrittenScope: +return P->SuppressUnwrittenScope; + case CXPrintingPolicy_SuppressInitializers: +return P->SuppressInitializers; + case CXPrintingPolicy_ConstantArraySizeAsWritten: +return P->ConstantArraySizeAsWritten; + case CXPrintingPolicy_AnonymousTagLocations: +return P->AnonymousTagLocations; + case CXPrintingPolicy_SuppressStrongLifetime: +return P->SuppressStrongLifetime; + case CXPrintingPolicy_SuppressLifetimeQualifiers: +return P->SuppressLifetimeQualifiers; + case CXPrintingPolicy_SuppressTemplateArgsInCXXConstructors: +return P->SuppressTemplateArgsInCXXConstructors; + case CXPrintingPolicy_Bool: +return P->Bool; + case CXPrintingPolicy_Restrict: +return P->Restrict; + case CXPrintingPolicy_Alignof: +return P->Alignof; + case CXPrintingPolicy_UnderscoreAlignof: +return P->UnderscoreAlignof; + case CXPrintingPolicy_UseVoidForZeroParams: +return P->UseVoidForZeroParams; + case CXPrintingPolicy_TerseOutput: +return P->TerseOutput; + case CXPrintingPolicy_PolishForDeclaration: +return P->PolishForDeclaration; + case CXPrintingPolicy_Half: +return P->Half; + case CXPrintingPolicy_MSWChar: +return P->MSWChar; + case CXPrintingPolicy_IncludeNewlines: +return P->IncludeNewlines; + case CXPrintingPolicy_MSVCFormatting: +return P->MSVCFormatting; + case CXPrintingPolicy_ConstantsAsWritten: +return P->ConstantsAsWritten; + case CXPrintingPolicy_SuppressImplicitBase: +return P->SuppressImplicitBase; + case CXPrintingPolicy_FullyQualifiedName: +return P->FullyQualifiedName; + } + + return 0; +} + +void clang_PrintingPolicy_setProperty(CXPrintingPolicy Policy, + enum CXPrintingPolicyPrope
[PATCH] D39903: [libclang] Allow pretty printing declarations
jbcoe added a comment. Looking good, only a few nits. Comment at: tools/libclang/CIndex.cpp:4782 + + return 0; +} Might be worth asserting here. Comment at: unittests/libclang/LibclangTest.cpp:596 +TEST_F(LibclangPrintingPolicyTest, GetProperty) { + EXPECT_EQ(2U, clang_PrintingPolicy_getProperty(Policy, CXPrintingPolicy_Indentation)); +} It would be useful, albeit tedious, to add get/set test pairs for each property. Repository: rC Clang https://reviews.llvm.org/D39903 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D39903: [libclang] Allow pretty printing declarations
nik marked an inline comment as done. nik added inline comments. Comment at: tools/libclang/CIndex.cpp:4782 + + return 0; +} jbcoe wrote: > Might be worth asserting here. Good idea. I've done the same for the setter. Comment at: unittests/libclang/LibclangTest.cpp:596 +TEST_F(LibclangPrintingPolicyTest, GetProperty) { + EXPECT_EQ(2U, clang_PrintingPolicy_getProperty(Policy, CXPrintingPolicy_Indentation)); +} jbcoe wrote: > It would be useful, albeit tedious, to add get/set test pairs for each > property. I think we have the basic functionality of the getter and setter covered. Testing each getter/setter for each property would mostly help to verify the mapping in the getters/setters - initially I've ruled out that possibility with the macros (once the macro is correct - but that's much easier to check). I would prefer to go back to the macro version than adding a test here that goes over all properties. Repository: rC Clang https://reviews.llvm.org/D39903 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D39903: [libclang] Allow pretty printing declarations
nik updated this revision to Diff 129600. nik marked an inline comment as done. nik added a comment. Added assert() for getter/setter. Repository: rC Clang https://reviews.llvm.org/D39903 Files: include/clang-c/Index.h test/Index/print-display-names.cpp tools/c-index-test/c-index-test.c tools/libclang/CIndex.cpp tools/libclang/libclang.exports unittests/libclang/LibclangTest.cpp Index: unittests/libclang/LibclangTest.cpp === --- unittests/libclang/LibclangTest.cpp +++ unittests/libclang/LibclangTest.cpp @@ -572,3 +572,26 @@ EXPECT_EQ(0U, clang_getNumDiagnostics(ClangTU)); DisplayDiagnostics(); } + +class LibclangPrintingPolicyTest : public LibclangParseTest { +public: + CXPrintingPolicy Policy = nullptr; + + void SetUp() override { +LibclangParseTest::SetUp(); +std::string File = "file.cpp"; +WriteFile(File, "int i;\n"); +ClangTU = clang_parseTranslationUnit(Index, File.c_str(), nullptr, 0, + nullptr, 0, TUFlags); +CXCursor TUCursor = clang_getTranslationUnitCursor(ClangTU); +Policy = clang_getCursorPrintingPolicy(TUCursor); + } + void TearDown() override { +clang_PrintingPolicy_dispose(Policy); +LibclangParseTest::TearDown(); + } +}; + +TEST_F(LibclangPrintingPolicyTest, GetProperty) { + EXPECT_EQ(2U, clang_PrintingPolicy_getProperty(Policy, CXPrintingPolicy_Indentation)); +} Index: tools/libclang/libclang.exports === --- tools/libclang/libclang.exports +++ tools/libclang/libclang.exports @@ -178,6 +178,8 @@ clang_getCursorCompletionString clang_getCursorDefinition clang_getCursorDisplayName +clang_getCursorPrintingPolicy +clang_getCursorPrettyPrinted clang_getCursorExtent clang_getCursorExceptionSpecificationType clang_getCursorKind @@ -359,3 +361,6 @@ clang_EvalResult_getAsDouble clang_EvalResult_getAsStr clang_EvalResult_dispose +clang_PrintingPolicy_getProperty +clang_PrintingPolicy_setProperty +clang_PrintingPolicy_dispose Index: tools/libclang/CIndex.cpp === --- tools/libclang/CIndex.cpp +++ tools/libclang/CIndex.cpp @@ -4706,6 +4706,195 @@ return cxstring::createSet(Manglings); } +CXPrintingPolicy clang_getCursorPrintingPolicy(CXCursor C) { + if (clang_Cursor_isNull(C)) +return 0; + return new PrintingPolicy(getCursorContext(C).getPrintingPolicy()); +} + +void clang_PrintingPolicy_dispose(CXPrintingPolicy Policy) { + if (Policy) +delete static_cast(Policy); +} + +unsigned +clang_PrintingPolicy_getProperty(CXPrintingPolicy Policy, + enum CXPrintingPolicyProperty Property) { + if (!Policy) +return 0; + + PrintingPolicy *P = static_cast(Policy); + switch (Property) { + case CXPrintingPolicy_Indentation: +return P->Indentation; + case CXPrintingPolicy_SuppressSpecifiers: +return P->SuppressSpecifiers; + case CXPrintingPolicy_SuppressTagKeyword: +return P->SuppressTagKeyword; + case CXPrintingPolicy_IncludeTagDefinition: +return P->IncludeTagDefinition; + case CXPrintingPolicy_SuppressScope: +return P->SuppressScope; + case CXPrintingPolicy_SuppressUnwrittenScope: +return P->SuppressUnwrittenScope; + case CXPrintingPolicy_SuppressInitializers: +return P->SuppressInitializers; + case CXPrintingPolicy_ConstantArraySizeAsWritten: +return P->ConstantArraySizeAsWritten; + case CXPrintingPolicy_AnonymousTagLocations: +return P->AnonymousTagLocations; + case CXPrintingPolicy_SuppressStrongLifetime: +return P->SuppressStrongLifetime; + case CXPrintingPolicy_SuppressLifetimeQualifiers: +return P->SuppressLifetimeQualifiers; + case CXPrintingPolicy_SuppressTemplateArgsInCXXConstructors: +return P->SuppressTemplateArgsInCXXConstructors; + case CXPrintingPolicy_Bool: +return P->Bool; + case CXPrintingPolicy_Restrict: +return P->Restrict; + case CXPrintingPolicy_Alignof: +return P->Alignof; + case CXPrintingPolicy_UnderscoreAlignof: +return P->UnderscoreAlignof; + case CXPrintingPolicy_UseVoidForZeroParams: +return P->UseVoidForZeroParams; + case CXPrintingPolicy_TerseOutput: +return P->TerseOutput; + case CXPrintingPolicy_PolishForDeclaration: +return P->PolishForDeclaration; + case CXPrintingPolicy_Half: +return P->Half; + case CXPrintingPolicy_MSWChar: +return P->MSWChar; + case CXPrintingPolicy_IncludeNewlines: +return P->IncludeNewlines; + case CXPrintingPolicy_MSVCFormatting: +return P->MSVCFormatting; + case CXPrintingPolicy_ConstantsAsWritten: +return P->ConstantsAsWritten; + case CXPrintingPolicy_SuppressImplicitBase: +return P->SuppressImplicitBase; + case CXPrintingPolicy_FullyQualifiedName: +return P->FullyQualifiedName; + } + + assert(false && "Invalid CXPrintingPolicyProperty"); + return 0; +} + +void clang_PrintingP
[PATCH] D39903: [libclang] Allow pretty printing declarations
jbcoe requested changes to this revision. jbcoe added inline comments. This revision now requires changes to proceed. Comment at: unittests/libclang/LibclangTest.cpp:596 +TEST_F(LibclangPrintingPolicyTest, GetProperty) { + EXPECT_EQ(2U, clang_PrintingPolicy_getProperty(Policy, CXPrintingPolicy_Indentation)); +} nik wrote: > jbcoe wrote: > > It would be useful, albeit tedious, to add get/set test pairs for each > > property. > I think we have the basic functionality of the getter and setter covered. > Testing each getter/setter for each property would mostly help to verify the > mapping in the getters/setters - initially I've ruled out that possibility > with the macros (once the macro is correct - but that's much easier to > check). I would prefer to go back to the macro version than adding a test > here that goes over all properties. I think the macro in implementation harms readability and the tests are pretty simple to add (maybe even using a macro). The current setup of a switch and no low-level tests leaves things open to breakage in future. Repository: rC Clang https://reviews.llvm.org/D39903 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D39903: [libclang] Allow pretty printing declarations
nik updated this revision to Diff 129635. nik added a comment. What about this? :) Repository: rC Clang https://reviews.llvm.org/D39903 Files: include/clang-c/Index.h test/Index/print-display-names.cpp tools/c-index-test/c-index-test.c tools/libclang/CIndex.cpp tools/libclang/libclang.exports unittests/libclang/LibclangTest.cpp Index: unittests/libclang/LibclangTest.cpp === --- unittests/libclang/LibclangTest.cpp +++ unittests/libclang/LibclangTest.cpp @@ -572,3 +572,33 @@ EXPECT_EQ(0U, clang_getNumDiagnostics(ClangTU)); DisplayDiagnostics(); } + +class LibclangPrintingPolicyTest : public LibclangParseTest { +public: + CXPrintingPolicy Policy = nullptr; + + void SetUp() override { +LibclangParseTest::SetUp(); +std::string File = "file.cpp"; +WriteFile(File, "int i;\n"); +ClangTU = clang_parseTranslationUnit(Index, File.c_str(), nullptr, 0, + nullptr, 0, TUFlags); +CXCursor TUCursor = clang_getTranslationUnitCursor(ClangTU); +Policy = clang_getCursorPrintingPolicy(TUCursor); + } + void TearDown() override { +clang_PrintingPolicy_dispose(Policy); +LibclangParseTest::TearDown(); + } +}; + +TEST_F(LibclangPrintingPolicyTest, SetAndGetProperties) { + for (unsigned Value = 0; Value < 2; ++Value) { +for (int I = 0; I < CXPrintingPolicy_LastProperty; ++I) { + auto Property = static_cast(I); + + clang_PrintingPolicy_setProperty(Policy, Property, Value); + EXPECT_EQ(Value, clang_PrintingPolicy_getProperty(Policy, Property)); +} + } +} Index: tools/libclang/libclang.exports === --- tools/libclang/libclang.exports +++ tools/libclang/libclang.exports @@ -178,6 +178,8 @@ clang_getCursorCompletionString clang_getCursorDefinition clang_getCursorDisplayName +clang_getCursorPrintingPolicy +clang_getCursorPrettyPrinted clang_getCursorExtent clang_getCursorExceptionSpecificationType clang_getCursorKind @@ -359,3 +361,6 @@ clang_EvalResult_getAsDouble clang_EvalResult_getAsStr clang_EvalResult_dispose +clang_PrintingPolicy_getProperty +clang_PrintingPolicy_setProperty +clang_PrintingPolicy_dispose Index: tools/libclang/CIndex.cpp === --- tools/libclang/CIndex.cpp +++ tools/libclang/CIndex.cpp @@ -4706,6 +4706,195 @@ return cxstring::createSet(Manglings); } +CXPrintingPolicy clang_getCursorPrintingPolicy(CXCursor C) { + if (clang_Cursor_isNull(C)) +return 0; + return new PrintingPolicy(getCursorContext(C).getPrintingPolicy()); +} + +void clang_PrintingPolicy_dispose(CXPrintingPolicy Policy) { + if (Policy) +delete static_cast(Policy); +} + +unsigned +clang_PrintingPolicy_getProperty(CXPrintingPolicy Policy, + enum CXPrintingPolicyProperty Property) { + if (!Policy) +return 0; + + PrintingPolicy *P = static_cast(Policy); + switch (Property) { + case CXPrintingPolicy_Indentation: +return P->Indentation; + case CXPrintingPolicy_SuppressSpecifiers: +return P->SuppressSpecifiers; + case CXPrintingPolicy_SuppressTagKeyword: +return P->SuppressTagKeyword; + case CXPrintingPolicy_IncludeTagDefinition: +return P->IncludeTagDefinition; + case CXPrintingPolicy_SuppressScope: +return P->SuppressScope; + case CXPrintingPolicy_SuppressUnwrittenScope: +return P->SuppressUnwrittenScope; + case CXPrintingPolicy_SuppressInitializers: +return P->SuppressInitializers; + case CXPrintingPolicy_ConstantArraySizeAsWritten: +return P->ConstantArraySizeAsWritten; + case CXPrintingPolicy_AnonymousTagLocations: +return P->AnonymousTagLocations; + case CXPrintingPolicy_SuppressStrongLifetime: +return P->SuppressStrongLifetime; + case CXPrintingPolicy_SuppressLifetimeQualifiers: +return P->SuppressLifetimeQualifiers; + case CXPrintingPolicy_SuppressTemplateArgsInCXXConstructors: +return P->SuppressTemplateArgsInCXXConstructors; + case CXPrintingPolicy_Bool: +return P->Bool; + case CXPrintingPolicy_Restrict: +return P->Restrict; + case CXPrintingPolicy_Alignof: +return P->Alignof; + case CXPrintingPolicy_UnderscoreAlignof: +return P->UnderscoreAlignof; + case CXPrintingPolicy_UseVoidForZeroParams: +return P->UseVoidForZeroParams; + case CXPrintingPolicy_TerseOutput: +return P->TerseOutput; + case CXPrintingPolicy_PolishForDeclaration: +return P->PolishForDeclaration; + case CXPrintingPolicy_Half: +return P->Half; + case CXPrintingPolicy_MSWChar: +return P->MSWChar; + case CXPrintingPolicy_IncludeNewlines: +return P->IncludeNewlines; + case CXPrintingPolicy_MSVCFormatting: +return P->MSVCFormatting; + case CXPrintingPolicy_ConstantsAsWritten: +return P->ConstantsAsWritten; + case CXPrintingPolicy_SuppressImplicitBase: +return P->SuppressImplicitBase; + case
[PATCH] D39903: [libclang] Allow pretty printing declarations
jbcoe added inline comments. Comment at: tools/c-index-test/c-index-test.c:93 +enum CXPrintingPolicyProperty property; + } mappings[] = { + {"CINDEXTEST_PRINTINGPOLICY_INDENTATION", CXPrintingPolicy_Indentation}, I'm not sure that joining the struct definition and variable definition like this gains much and does not aid readability. Repository: rC Clang https://reviews.llvm.org/D39903 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D39903: [libclang] Allow pretty printing declarations
nik updated this revision to Diff 129805. nik added a comment. Addressed inline comment. Repository: rC Clang https://reviews.llvm.org/D39903 Files: include/clang-c/Index.h test/Index/print-display-names.cpp tools/c-index-test/c-index-test.c tools/libclang/CIndex.cpp tools/libclang/libclang.exports unittests/libclang/LibclangTest.cpp Index: unittests/libclang/LibclangTest.cpp === --- unittests/libclang/LibclangTest.cpp +++ unittests/libclang/LibclangTest.cpp @@ -572,3 +572,33 @@ EXPECT_EQ(0U, clang_getNumDiagnostics(ClangTU)); DisplayDiagnostics(); } + +class LibclangPrintingPolicyTest : public LibclangParseTest { +public: + CXPrintingPolicy Policy = nullptr; + + void SetUp() override { +LibclangParseTest::SetUp(); +std::string File = "file.cpp"; +WriteFile(File, "int i;\n"); +ClangTU = clang_parseTranslationUnit(Index, File.c_str(), nullptr, 0, + nullptr, 0, TUFlags); +CXCursor TUCursor = clang_getTranslationUnitCursor(ClangTU); +Policy = clang_getCursorPrintingPolicy(TUCursor); + } + void TearDown() override { +clang_PrintingPolicy_dispose(Policy); +LibclangParseTest::TearDown(); + } +}; + +TEST_F(LibclangPrintingPolicyTest, SetAndGetProperties) { + for (unsigned Value = 0; Value < 2; ++Value) { +for (int I = 0; I < CXPrintingPolicy_LastProperty; ++I) { + auto Property = static_cast(I); + + clang_PrintingPolicy_setProperty(Policy, Property, Value); + EXPECT_EQ(Value, clang_PrintingPolicy_getProperty(Policy, Property)); +} + } +} Index: tools/libclang/libclang.exports === --- tools/libclang/libclang.exports +++ tools/libclang/libclang.exports @@ -178,6 +178,8 @@ clang_getCursorCompletionString clang_getCursorDefinition clang_getCursorDisplayName +clang_getCursorPrintingPolicy +clang_getCursorPrettyPrinted clang_getCursorExtent clang_getCursorExceptionSpecificationType clang_getCursorKind @@ -359,3 +361,6 @@ clang_EvalResult_getAsDouble clang_EvalResult_getAsStr clang_EvalResult_dispose +clang_PrintingPolicy_getProperty +clang_PrintingPolicy_setProperty +clang_PrintingPolicy_dispose Index: tools/libclang/CIndex.cpp === --- tools/libclang/CIndex.cpp +++ tools/libclang/CIndex.cpp @@ -4706,6 +4706,195 @@ return cxstring::createSet(Manglings); } +CXPrintingPolicy clang_getCursorPrintingPolicy(CXCursor C) { + if (clang_Cursor_isNull(C)) +return 0; + return new PrintingPolicy(getCursorContext(C).getPrintingPolicy()); +} + +void clang_PrintingPolicy_dispose(CXPrintingPolicy Policy) { + if (Policy) +delete static_cast(Policy); +} + +unsigned +clang_PrintingPolicy_getProperty(CXPrintingPolicy Policy, + enum CXPrintingPolicyProperty Property) { + if (!Policy) +return 0; + + PrintingPolicy *P = static_cast(Policy); + switch (Property) { + case CXPrintingPolicy_Indentation: +return P->Indentation; + case CXPrintingPolicy_SuppressSpecifiers: +return P->SuppressSpecifiers; + case CXPrintingPolicy_SuppressTagKeyword: +return P->SuppressTagKeyword; + case CXPrintingPolicy_IncludeTagDefinition: +return P->IncludeTagDefinition; + case CXPrintingPolicy_SuppressScope: +return P->SuppressScope; + case CXPrintingPolicy_SuppressUnwrittenScope: +return P->SuppressUnwrittenScope; + case CXPrintingPolicy_SuppressInitializers: +return P->SuppressInitializers; + case CXPrintingPolicy_ConstantArraySizeAsWritten: +return P->ConstantArraySizeAsWritten; + case CXPrintingPolicy_AnonymousTagLocations: +return P->AnonymousTagLocations; + case CXPrintingPolicy_SuppressStrongLifetime: +return P->SuppressStrongLifetime; + case CXPrintingPolicy_SuppressLifetimeQualifiers: +return P->SuppressLifetimeQualifiers; + case CXPrintingPolicy_SuppressTemplateArgsInCXXConstructors: +return P->SuppressTemplateArgsInCXXConstructors; + case CXPrintingPolicy_Bool: +return P->Bool; + case CXPrintingPolicy_Restrict: +return P->Restrict; + case CXPrintingPolicy_Alignof: +return P->Alignof; + case CXPrintingPolicy_UnderscoreAlignof: +return P->UnderscoreAlignof; + case CXPrintingPolicy_UseVoidForZeroParams: +return P->UseVoidForZeroParams; + case CXPrintingPolicy_TerseOutput: +return P->TerseOutput; + case CXPrintingPolicy_PolishForDeclaration: +return P->PolishForDeclaration; + case CXPrintingPolicy_Half: +return P->Half; + case CXPrintingPolicy_MSWChar: +return P->MSWChar; + case CXPrintingPolicy_IncludeNewlines: +return P->IncludeNewlines; + case CXPrintingPolicy_MSVCFormatting: +return P->MSVCFormatting; + case CXPrintingPolicy_ConstantsAsWritten: +return P->ConstantsAsWritten; + case CXPrintingPolicy_SuppressImplicitBase: +return P->SuppressImplicitBase; +
[PATCH] D39903: [libclang] Allow pretty printing declarations
nik added a comment. Can you submit this for me? I don't have the permissions. Repository: rC Clang https://reviews.llvm.org/D39903 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D39903: [libclang] Allow pretty printing declarations
jbcoe added a comment. I can merge this for you. Please add me as reviewer in any follow-up patches and we can turn them around more quickly. Repository: rC Clang https://reviews.llvm.org/D39903 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D39903: [libclang] Allow pretty printing declarations
nik added a comment. > I can merge this for you. > Please add me as reviewer in any follow-up patches and we can turn them > around more quickly. That would be nice, thanks! I don't have any follow-up patches right now. Repository: rC Clang https://reviews.llvm.org/D39903 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D39903: [libclang] Allow pretty printing declarations
This revision was automatically updated to reflect the committed changes. Closed by commit rL322540: [libclang] Add PrintingPolicy for pretty printing declarations (authored by jbcoe, committed by ). Herald added a subscriber: llvm-commits. Changed prior to commit: https://reviews.llvm.org/D39903?vs=129805&id=129926#toc Repository: rL LLVM https://reviews.llvm.org/D39903 Files: cfe/trunk/include/clang-c/Index.h cfe/trunk/test/Index/print-display-names.cpp cfe/trunk/tools/c-index-test/c-index-test.c cfe/trunk/tools/libclang/CIndex.cpp cfe/trunk/tools/libclang/libclang.exports cfe/trunk/unittests/libclang/LibclangTest.cpp Index: cfe/trunk/unittests/libclang/LibclangTest.cpp === --- cfe/trunk/unittests/libclang/LibclangTest.cpp +++ cfe/trunk/unittests/libclang/LibclangTest.cpp @@ -573,6 +573,36 @@ DisplayDiagnostics(); } +class LibclangPrintingPolicyTest : public LibclangParseTest { +public: + CXPrintingPolicy Policy = nullptr; + + void SetUp() override { +LibclangParseTest::SetUp(); +std::string File = "file.cpp"; +WriteFile(File, "int i;\n"); +ClangTU = clang_parseTranslationUnit(Index, File.c_str(), nullptr, 0, + nullptr, 0, TUFlags); +CXCursor TUCursor = clang_getTranslationUnitCursor(ClangTU); +Policy = clang_getCursorPrintingPolicy(TUCursor); + } + void TearDown() override { +clang_PrintingPolicy_dispose(Policy); +LibclangParseTest::TearDown(); + } +}; + +TEST_F(LibclangPrintingPolicyTest, SetAndGetProperties) { + for (unsigned Value = 0; Value < 2; ++Value) { +for (int I = 0; I < CXPrintingPolicy_LastProperty; ++I) { + auto Property = static_cast(I); + + clang_PrintingPolicy_setProperty(Policy, Property, Value); + EXPECT_EQ(Value, clang_PrintingPolicy_getProperty(Policy, Property)); +} + } +} + TEST_F(LibclangReparseTest, PreprocessorSkippedRanges) { std::string Header = "header.h", Main = "main.cpp"; WriteFile(Header, Index: cfe/trunk/tools/libclang/CIndex.cpp === --- cfe/trunk/tools/libclang/CIndex.cpp +++ cfe/trunk/tools/libclang/CIndex.cpp @@ -4706,6 +4706,195 @@ return cxstring::createSet(Manglings); } +CXPrintingPolicy clang_getCursorPrintingPolicy(CXCursor C) { + if (clang_Cursor_isNull(C)) +return 0; + return new PrintingPolicy(getCursorContext(C).getPrintingPolicy()); +} + +void clang_PrintingPolicy_dispose(CXPrintingPolicy Policy) { + if (Policy) +delete static_cast(Policy); +} + +unsigned +clang_PrintingPolicy_getProperty(CXPrintingPolicy Policy, + enum CXPrintingPolicyProperty Property) { + if (!Policy) +return 0; + + PrintingPolicy *P = static_cast(Policy); + switch (Property) { + case CXPrintingPolicy_Indentation: +return P->Indentation; + case CXPrintingPolicy_SuppressSpecifiers: +return P->SuppressSpecifiers; + case CXPrintingPolicy_SuppressTagKeyword: +return P->SuppressTagKeyword; + case CXPrintingPolicy_IncludeTagDefinition: +return P->IncludeTagDefinition; + case CXPrintingPolicy_SuppressScope: +return P->SuppressScope; + case CXPrintingPolicy_SuppressUnwrittenScope: +return P->SuppressUnwrittenScope; + case CXPrintingPolicy_SuppressInitializers: +return P->SuppressInitializers; + case CXPrintingPolicy_ConstantArraySizeAsWritten: +return P->ConstantArraySizeAsWritten; + case CXPrintingPolicy_AnonymousTagLocations: +return P->AnonymousTagLocations; + case CXPrintingPolicy_SuppressStrongLifetime: +return P->SuppressStrongLifetime; + case CXPrintingPolicy_SuppressLifetimeQualifiers: +return P->SuppressLifetimeQualifiers; + case CXPrintingPolicy_SuppressTemplateArgsInCXXConstructors: +return P->SuppressTemplateArgsInCXXConstructors; + case CXPrintingPolicy_Bool: +return P->Bool; + case CXPrintingPolicy_Restrict: +return P->Restrict; + case CXPrintingPolicy_Alignof: +return P->Alignof; + case CXPrintingPolicy_UnderscoreAlignof: +return P->UnderscoreAlignof; + case CXPrintingPolicy_UseVoidForZeroParams: +return P->UseVoidForZeroParams; + case CXPrintingPolicy_TerseOutput: +return P->TerseOutput; + case CXPrintingPolicy_PolishForDeclaration: +return P->PolishForDeclaration; + case CXPrintingPolicy_Half: +return P->Half; + case CXPrintingPolicy_MSWChar: +return P->MSWChar; + case CXPrintingPolicy_IncludeNewlines: +return P->IncludeNewlines; + case CXPrintingPolicy_MSVCFormatting: +return P->MSVCFormatting; + case CXPrintingPolicy_ConstantsAsWritten: +return P->ConstantsAsWritten; + case CXPrintingPolicy_SuppressImplicitBase: +return P->SuppressImplicitBase; + case CXPrintingPolicy_FullyQualifiedName: +return P->FullyQualifiedName; + } + + assert(false && "Invalid CXPrintingPolicyProperty"); + return 0; +} + +void clang_PrintingPolicy_setProperty
[PATCH] D39903: [libclang] Allow pretty printing declarations
nik added a comment. Ping, please review :) https://reviews.llvm.org/D39903 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D39903: [libclang] Allow pretty printing declarations
nik added a comment. Ping, please review or add more appropriate reviewers. https://reviews.llvm.org/D39903 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D39903: [libclang] Allow pretty printing declarations
nik added a comment. Ping III - is there anything I can do to get this reviewed faster? 3 weeks passed. https://reviews.llvm.org/D39903 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D39903: [libclang] Allow pretty printing declarations
nik added reviewers: ilya-biryukov, cameron314. nik added a comment. Anyone? https://reviews.llvm.org/D39903 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D39903: [libclang] Allow pretty printing declarations
nik updated this revision to Diff 125129. nik added a comment. Rebased only. Repository: rC Clang https://reviews.llvm.org/D39903 Files: include/clang-c/Index.h test/Index/print-display-names.cpp tools/c-index-test/c-index-test.c tools/libclang/CIndex.cpp tools/libclang/libclang.exports Index: tools/libclang/libclang.exports === --- tools/libclang/libclang.exports +++ tools/libclang/libclang.exports @@ -176,6 +176,8 @@ clang_getCursorCompletionString clang_getCursorDefinition clang_getCursorDisplayName +clang_getCursorPrintingPolicy +clang_getCursorPrettyPrinted clang_getCursorExtent clang_getCursorExceptionSpecificationType clang_getCursorKind @@ -355,3 +357,56 @@ clang_EvalResult_getAsDouble clang_EvalResult_getAsStr clang_EvalResult_dispose +clang_PrintingPolicy_dispose +clang_PrintingPolicy_getIndentation +clang_PrintingPolicy_getSuppressSpecifiers +clang_PrintingPolicy_getSuppressTagKeyword +clang_PrintingPolicy_getIncludeTagDefinition +clang_PrintingPolicy_getSuppressScope +clang_PrintingPolicy_getSuppressUnwrittenScope +clang_PrintingPolicy_getSuppressInitializers +clang_PrintingPolicy_getConstantArraySizeAsWritten +clang_PrintingPolicy_getAnonymousTagLocations +clang_PrintingPolicy_getSuppressStrongLifetime +clang_PrintingPolicy_getSuppressLifetimeQualifiers +clang_PrintingPolicy_getSuppressTemplateArgsInCXXConstructors +clang_PrintingPolicy_getBool +clang_PrintingPolicy_getRestrict +clang_PrintingPolicy_getAlignof +clang_PrintingPolicy_getUnderscoreAlignof +clang_PrintingPolicy_getUseVoidForZeroParams +clang_PrintingPolicy_getTerseOutput +clang_PrintingPolicy_getPolishForDeclaration +clang_PrintingPolicy_getHalf +clang_PrintingPolicy_getMSWChar +clang_PrintingPolicy_getIncludeNewlines +clang_PrintingPolicy_getMSVCFormatting +clang_PrintingPolicy_getConstantsAsWritten +clang_PrintingPolicy_getSuppressImplicitBase +clang_PrintingPolicy_getFullyQualifiedName +clang_PrintingPolicy_setIndentation +clang_PrintingPolicy_setSuppressSpecifiers +clang_PrintingPolicy_setSuppressTagKeyword +clang_PrintingPolicy_setIncludeTagDefinition +clang_PrintingPolicy_setSuppressScope +clang_PrintingPolicy_setSuppressUnwrittenScope +clang_PrintingPolicy_setSuppressInitializers +clang_PrintingPolicy_setConstantArraySizeAsWritten +clang_PrintingPolicy_setAnonymousTagLocations +clang_PrintingPolicy_setSuppressStrongLifetime +clang_PrintingPolicy_setSuppressLifetimeQualifiers +clang_PrintingPolicy_setSuppressTemplateArgsInCXXConstructors +clang_PrintingPolicy_setBool +clang_PrintingPolicy_setRestrict +clang_PrintingPolicy_setAlignof +clang_PrintingPolicy_setUnderscoreAlignof +clang_PrintingPolicy_setUseVoidForZeroParams +clang_PrintingPolicy_setTerseOutput +clang_PrintingPolicy_setPolishForDeclaration +clang_PrintingPolicy_setHalf +clang_PrintingPolicy_setMSWChar +clang_PrintingPolicy_setIncludeNewlines +clang_PrintingPolicy_setMSVCFormatting +clang_PrintingPolicy_setConstantsAsWritten +clang_PrintingPolicy_setSuppressImplicitBase +clang_PrintingPolicy_setFullyQualifiedName Index: tools/libclang/CIndex.cpp === --- tools/libclang/CIndex.cpp +++ tools/libclang/CIndex.cpp @@ -4649,6 +4649,107 @@ return cxstring::createSet(Manglings); } +CXPrintingPolicy clang_getCursorPrintingPolicy(CXCursor C) { + if (clang_Cursor_isNull(C)) +return 0; + return new PrintingPolicy(getCursorContext(C).getPrintingPolicy()); +} + +void clang_PrintingPolicy_dispose(CXPrintingPolicy policy) { + if (policy) +delete static_cast(policy); +} + +#define DEFINE_PRINTING_POLICY_GETTER(name)\ + unsigned clang_PrintingPolicy_get##name(CXPrintingPolicy policy) { \ +if (!policy) \ + return 0;\ +return static_cast(policy)->name;\ + } +DEFINE_PRINTING_POLICY_GETTER(Indentation) +DEFINE_PRINTING_POLICY_GETTER(SuppressSpecifiers) +DEFINE_PRINTING_POLICY_GETTER(SuppressTagKeyword) +DEFINE_PRINTING_POLICY_GETTER(IncludeTagDefinition) +DEFINE_PRINTING_POLICY_GETTER(SuppressScope) +DEFINE_PRINTING_POLICY_GETTER(SuppressUnwrittenScope) +DEFINE_PRINTING_POLICY_GETTER(SuppressInitializers) +DEFINE_PRINTING_POLICY_GETTER(ConstantArraySizeAsWritten) +DEFINE_PRINTING_POLICY_GETTER(AnonymousTagLocations) +DEFINE_PRINTING_POLICY_GETTER(SuppressStrongLifetime) +DEFINE_PRINTING_POLICY_GETTER(SuppressLifetimeQualifiers) +DEFINE_PRINTING_POLICY_GETTER(SuppressTemplateArgsInCXXConstructors) +DEFINE_PRINTING_POLICY_GETTER(Bool) +DEFINE_PRINTING_POLICY_GETTER(Restrict) +DEFINE_PRINTING_POLICY_GETTER(Alignof) +DEFINE_PRINTING_POLICY_GETTER(UnderscoreAlignof) +DEFINE_PRINTING_POLICY_GETTER(UseVoidForZeroParams) +DEFINE_PRINTING_POLICY_GETTER(TerseOutput) +DEFINE_PRINTING_POLICY_GETTER(PolishForDeclaration) +D
[PATCH] D39903: [libclang] Allow pretty printing declarations
cameron314 added a comment. Locally we've done something similar (adding a `clang_getCursorPrettyPrintedDeclaration` function, though without exposing the `PrintingPolicy`) and overhauled `DeclPrinter` to produce proper pretty names. This is a hack that works well for us, but can never be upstreamed since it breaks too much existing code (and some of the printing decisions are subjective). Your way is better. I can point out differences in our implementations if you like. The diff is rather long, though. Repository: rC Clang https://reviews.llvm.org/D39903 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D39903: [libclang] Allow pretty printing declarations
nik added a comment. In https://reviews.llvm.org/D39903#944182, @cameron314 wrote: > Locally we've done something similar (adding a > `clang_getCursorPrettyPrintedDeclaration` function, though without exposing > the `PrintingPolicy`) and overhauled `DeclPrinter` to produce proper pretty > names. This is a hack that works well for us, but can never be upstreamed > since it breaks too much existing code (and some of the printing decisions > are subjective). Your way is better. You might consider to enhance PrintingPolicy for your use cases? > I can point out differences in our implementations if you like. The diff is > rather long, though. That would be interesting, yes, but rather later. First I would like to get a review for this one... Ping. Repository: rC Clang https://reviews.llvm.org/D39903 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits