You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ASTStringUtilExt.java 6.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package depends.extractor.cpp.cdt;
  2. import depends.entity.GenericName;
  3. import depends.entity.TypeEntity;
  4. import org.eclipse.cdt.core.dom.ast.*;
  5. import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNameSpecifier;
  6. import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
  7. import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTUsingDeclaration;
  8. import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTTemplateId;
  9. import org.eclipse.cdt.internal.core.model.ASTStringUtil;
  10. import java.lang.reflect.InvocationTargetException;
  11. import java.lang.reflect.Method;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. /**
  15. * This extends the CDT ASTStringUtil class.
  16. * A tricky point here is that we have to use some of the reflection mechanism to invoke
  17. * some private functions in ASTStringUtils class
  18. * It is not good, but it seems the most easiest one to reuse existing functions
  19. */
  20. public class ASTStringUtilExt extends ASTStringUtil {
  21. public static String getName(IASTDeclSpecifier decl) {
  22. StringBuilder buffer = new StringBuilder();
  23. String name = appendBareDeclSpecifierString(buffer, decl).toString().replace("::", ".").replace("...", "");
  24. return name;
  25. }
  26. public static String getName(IASTLiteralExpression expr) {
  27. return expr.getRawSignature().replace("::", ".").replace("...", "");
  28. }
  29. public static String getTypeIdString(IASTTypeId typeId) {
  30. StringBuilder sb = new StringBuilder();
  31. return appendBareTypeIdString(sb, typeId).toString().replace("::", ".");
  32. }
  33. /**
  34. * retrieve template parameters from declSpecifier
  35. */
  36. public static List<GenericName> getTemplateParameters(IASTDeclSpecifier declSpecifier) {
  37. List<GenericName> parameters = new ArrayList<>();
  38. declSpecifier.accept(new TemplateParameterASTVisitor(parameters));
  39. return parameters;
  40. }
  41. private static StringBuilder appendBareDeclSpecifierString(StringBuilder buffer, IASTDeclSpecifier declSpecifier) {
  42. if (declSpecifier instanceof IASTCompositeTypeSpecifier) {
  43. final IASTCompositeTypeSpecifier compositeTypeSpec = (IASTCompositeTypeSpecifier) declSpecifier;
  44. appendBareNameString(buffer, compositeTypeSpec.getName());
  45. } else if (declSpecifier instanceof IASTElaboratedTypeSpecifier) {
  46. final IASTElaboratedTypeSpecifier elaboratedTypeSpec = (IASTElaboratedTypeSpecifier) declSpecifier;
  47. appendBareNameString(buffer, elaboratedTypeSpec.getName());
  48. } else if (declSpecifier instanceof IASTEnumerationSpecifier) {
  49. final IASTEnumerationSpecifier enumerationSpec = (IASTEnumerationSpecifier) declSpecifier;
  50. appendBareNameString(buffer, enumerationSpec.getName());
  51. } else if (declSpecifier instanceof IASTSimpleDeclSpecifier) {
  52. buffer.append(TypeEntity.buildInType.getRawName());
  53. } else if (declSpecifier instanceof IASTNamedTypeSpecifier) {
  54. final IASTNamedTypeSpecifier namedTypeSpec = (IASTNamedTypeSpecifier) declSpecifier;
  55. appendBareNameString(buffer, namedTypeSpec.getName());
  56. }
  57. return buffer;
  58. }
  59. private static StringBuilder appendBareNameString(StringBuilder buffer, IASTName name) {
  60. if (name instanceof ICPPASTQualifiedName) {
  61. final ICPPASTQualifiedName qualifiedName = (ICPPASTQualifiedName) name;
  62. final ICPPASTNameSpecifier[] segments = qualifiedName.getAllSegments();
  63. for (int i = 0; i < segments.length; i++) {
  64. if (i > 0) {
  65. buffer.append(".");
  66. }
  67. appendQualifiedNameStringWithReflection(buffer, segments[i]);
  68. }
  69. } else if (name instanceof CPPASTTemplateId) {
  70. appendQualifiedNameStringWithReflection(buffer,(CPPASTTemplateId)name);
  71. } else if (name != null) {
  72. buffer.append(name.getSimpleID());
  73. }
  74. return buffer;
  75. }
  76. private static void appendQualifiedNameStringWithReflection(StringBuilder buffer, IASTName name) {
  77. try {
  78. Method m = ASTStringUtil.class.getDeclaredMethod("appendQualifiedNameString", StringBuilder.class,
  79. IASTName.class);
  80. m.setAccessible(true); // if security settings allow this
  81. m.invoke(null, buffer, name); // use null if the method is static
  82. } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
  83. | InvocationTargetException e) {
  84. System.err.println("Error: cannot invoke ASTStringUtils method of <appendQualifiedNameString>");
  85. }
  86. }
  87. private static void appendQualifiedNameStringWithReflection(StringBuilder buffer,
  88. CPPASTTemplateId templateId) {
  89. appendQualifiedNameStringWithReflection(buffer,templateId.getTemplateName());
  90. }
  91. private static void appendQualifiedNameStringWithReflection(StringBuilder buffer,
  92. ICPPASTNameSpecifier nameSpecifier) {
  93. if (nameSpecifier instanceof CPPASTTemplateId) {
  94. appendQualifiedNameStringWithReflection(buffer,(CPPASTTemplateId)nameSpecifier);
  95. return;
  96. }
  97. try {
  98. Method m = ASTStringUtil.class.getDeclaredMethod("appendQualifiedNameString", StringBuilder.class,
  99. ICPPASTNameSpecifier.class);
  100. m.setAccessible(true); // if security settings allow this
  101. m.invoke(null, buffer, nameSpecifier); // use null if the method is static
  102. } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
  103. | InvocationTargetException e) {
  104. System.err.println("Error: cannot invoke ASTStringUtils method of <appendQualifiedNameString>");
  105. }
  106. }
  107. private static StringBuilder appendBareTypeIdString(StringBuilder buffer, IASTTypeId typeId) {
  108. return appendBareDeclSpecifierString(buffer, typeId.getDeclSpecifier());
  109. }
  110. public static String getName(IASTDeclarator declarator) {
  111. return declarator.getName().toString().replace("::", ".");
  112. }
  113. public static String getName(ICPPASTUsingDeclaration declaration) {
  114. return declaration.getName().toString().replace("::", ".");
  115. }
  116. public static String getName(IASTName name) {
  117. return name.getRawSignature().toString().replace("::", ".");
  118. }
  119. private static StringBuilder appendBareNameString(StringBuilder buffer, ICPPASTNameSpecifier name) {
  120. if (name instanceof ICPPASTQualifiedName) {
  121. final ICPPASTQualifiedName qualifiedName = (ICPPASTQualifiedName) name;
  122. final ICPPASTNameSpecifier[] segments = qualifiedName.getAllSegments();
  123. for (int i = 0; i < segments.length; i++) {
  124. if (i > 0) {
  125. buffer.append(".");
  126. }
  127. appendQualifiedNameStringWithReflection(buffer, segments[i]);
  128. }
  129. } else if (name instanceof CPPASTTemplateId) {
  130. appendQualifiedNameStringWithReflection(buffer,(CPPASTTemplateId)name);
  131. } else if (name != null) {
  132. buffer.append(name.getRawSignature());
  133. }
  134. return buffer;
  135. }
  136. public static String getName(ICPPASTNameSpecifier nameSpecifier) {
  137. StringBuilder buffer = new StringBuilder();
  138. String name = appendBareNameString(buffer, nameSpecifier).toString().replace("::", ".").replace("...", "");
  139. return name;
  140. }
  141. }

人工智能研发终端

Contributors (2)