Beispiel #1
0
  public boolean forFriendship() {
    if (astName == null) return false;
    IASTNode node = astName.getParent();
    while (node instanceof IASTName) node = node.getParent();

    IASTDeclaration decl = null;
    IASTDeclarator dtor = null;
    if (node instanceof ICPPASTDeclSpecifier && node.getParent() instanceof IASTDeclaration) {
      decl = (IASTDeclaration) node.getParent();
    } else if (node instanceof IASTDeclarator) {
      dtor = (IASTDeclarator) node;
      while (dtor.getParent() instanceof IASTDeclarator) dtor = (IASTDeclarator) dtor.getParent();
      if (!(dtor.getParent() instanceof IASTDeclaration)) return false;
      decl = (IASTDeclaration) dtor.getParent();
    } else {
      return false;
    }
    if (decl instanceof IASTSimpleDeclaration) {
      IASTSimpleDeclaration simple = (IASTSimpleDeclaration) decl;
      if (!((ICPPASTDeclSpecifier) simple.getDeclSpecifier()).isFriend()) return false;
      if (dtor != null) return true;
      return simple.getDeclarators().length == 0;
    } else if (decl instanceof IASTFunctionDefinition) {
      IASTFunctionDefinition fnDef = (IASTFunctionDefinition) decl;
      if (!((ICPPASTDeclSpecifier) fnDef.getDeclSpecifier()).isFriend()) return false;
      return (dtor != null);
    }
    return false;
  }
 public void testObjectMacroExpansionPartialDeclSpec() throws Exception {
   StringBuffer buffer = new StringBuffer("#define XYZ const\n"); // $NON-NLS-1$
   buffer.append("XYZ int var;"); // $NON-NLS-1$
   String code = buffer.toString();
   for (ParserLanguage language : languages) {
     IASTTranslationUnit tu = parse(code, language);
     IASTPreprocessorObjectStyleMacroDefinition defXYZ =
         (IASTPreprocessorObjectStyleMacroDefinition) tu.getMacroDefinitions()[0];
     IASTSimpleDeclaration var = (IASTSimpleDeclaration) tu.getDeclarations()[0];
     IASTSimpleDeclSpecifier declSpec = (IASTSimpleDeclSpecifier) var.getDeclSpecifier();
     IASTNodeLocation[] declSpecLocations = declSpec.getNodeLocations();
     assertEquals(declSpecLocations.length, 2);
     IASTMacroExpansionLocation expansion = (IASTMacroExpansionLocation) declSpecLocations[0];
     assertEqualsMacros(defXYZ, expansion.getExpansion().getMacroDefinition());
     assertEquals(expansion.getNodeOffset(), 0);
     assertEquals(expansion.getNodeLength(), 1);
     IASTNodeLocation[] expansionLocations = expansion.getExpansion().getNodeLocations();
     assertEquals(expansionLocations.length, 1);
     assertTrue(expansionLocations[0] instanceof IASTFileLocation);
     assertEquals(expansionLocations[0].getNodeOffset(), code.indexOf("XYZ int")); // $NON-NLS-1$
     assertEquals(expansionLocations[0].getNodeLength(), "XYZ".length()); // $NON-NLS-1$
     IASTFileLocation second = (IASTFileLocation) declSpecLocations[1];
     assertEquals(second.getNodeOffset(), code.indexOf(" int")); // $NON-NLS-1$
     assertEquals(second.getNodeLength(), " int".length()); // $NON-NLS-1$
   }
 }
Beispiel #3
0
 // typedef int tint;
 // struct X {
 //    int a;
 // } c
 // tint b;
 public void testCompositeTypeWithDtorWithoutSemi() throws Exception {
   final String comment = getAboveComment();
   for (ParserLanguage lang : ParserLanguage.values()) {
     IASTTranslationUnit tu = parse(comment, lang, false, false);
     IASTSimpleDeclaration sdecl = getDeclaration(tu, 1);
     assertInstance(sdecl.getDeclSpecifier(), IASTCompositeTypeSpecifier.class);
     assertEquals(1, sdecl.getDeclarators().length);
     IASTProblemDeclaration pdecl = getDeclaration(tu, 2);
     sdecl = getDeclaration(tu, 3);
   }
 }
Beispiel #4
0
  @Override
  public int getRoleForName(IASTName n) {
    // 3.1.2
    IASTNode parent = ASTQueries.findOutermostDeclarator(this).getParent();
    if (parent instanceof IASTDeclaration) {
      // a declaration is a definition unless ...
      if (parent instanceof IASTFunctionDefinition) return r_definition;

      if (parent instanceof IASTSimpleDeclaration) {
        final IASTSimpleDeclaration sdecl = (IASTSimpleDeclaration) parent;

        // unless it declares a function without body
        if (this instanceof IASTFunctionDeclarator) {
          return r_declaration;
        }

        final int storage = sdecl.getDeclSpecifier().getStorageClass();
        // unless it contains the extern specifier or a linkage-specification and neither
        // initializer nor function-body
        if (getInitializer() == null
            && (storage == IASTDeclSpecifier.sc_extern || isSimpleLinkageSpec(sdecl))) {
          return r_declaration;
        }
        // unless it declares a static data member in a class declaration
        if (storage == IASTDeclSpecifier.sc_static
            && CPPVisitor.getContainingScope(parent) instanceof ICPPClassScope) {
          return r_declaration;
        }
        // unless it is a class name declaration: no declarator in this case
        // unless it is a typedef declaration
        if (storage == IASTDeclSpecifier.sc_typedef)
          return r_definition; // should actually be a declaration

        // unless it is a using-declaration or using-directive: no declarator in this case
      }

      // all other cases
      return r_definition;
    }

    if (parent instanceof IASTTypeId) return r_reference;

    if (parent instanceof IASTParameterDeclaration)
      return (n.getLookupKey().length > 0) ? r_definition : r_declaration;

    return r_unclear;
  }
  private void visitType(IASTSimpleDeclaration declaration) throws BadLocationException {
    /* TODO: specific params: include type('class' / 'struct') */

    IASTDeclSpecifier spec = declaration.getDeclSpecifier();
    if (spec instanceof IASTCompositeTypeSpecifier) {
      String hint = ((IASTCompositeTypeSpecifier) spec).getName().getRawSignature();
      if (hint.isEmpty()) return;

      IASTFileLocation location = declaration.getFileLocation();
      int endLoc = location.getNodeOffset() + location.getNodeLength() - 1;
      int startLoc = location.getNodeOffset();
      _container.add(new Hint("type", startLoc, endLoc, hint)); // $NON-NLS-1$
    }

    if (spec instanceof ICPPASTNamedTypeSpecifier) {
      IASTName name = ((ICPPASTNamedTypeSpecifier) spec).getName();
      addBrackets(name);
    }
  }
 /*
  * @see org.eclipse.cdt.core.dom.ast.ASTVisitor#leave(org.eclipse.cdt.core.dom.ast.IASTDeclaration)
  */
 @Override
 public int leave(IASTDeclaration node) {
   super.leave(node);
   boolean isTemplateDecl = isTemplateDecl(node);
   final int endOffset = isTemplateDecl ? getEndOffset(node.getParent()) : getEndOffset(node);
   if (node instanceof IASTFunctionDefinition) {
     final int nodeType;
     if (inClassBody()) {
       nodeType = isTemplateDecl ? ICElement.C_TEMPLATE_METHOD : ICElement.C_METHOD;
     } else {
       nodeType = isTemplateDecl ? ICElement.C_TEMPLATE_FUNCTION : ICElement.C_FUNCTION;
     }
     assert getCurrentContainer().getTypeCode() == nodeType;
     pop(endOffset);
   } else if (node instanceof IASTSimpleDeclaration) {
     IASTSimpleDeclaration simpleDecl = (IASTSimpleDeclaration) node;
     IASTDeclSpecifier declSpec = simpleDecl.getDeclSpecifier();
     boolean isCompositeType = false;
     if (declSpec instanceof ICPPASTCompositeTypeSpecifier) {
       isCompositeType = true;
       ICPPASTCompositeTypeSpecifier compositeTypeSpec = (ICPPASTCompositeTypeSpecifier) declSpec;
       final int nodeType;
       switch (compositeTypeSpec.getKey()) {
         case IASTCompositeTypeSpecifier.k_struct:
           nodeType = isTemplateDecl ? ICElement.C_TEMPLATE_STRUCT : ICElement.C_STRUCT;
           break;
         case IASTCompositeTypeSpecifier.k_union:
           nodeType = isTemplateDecl ? ICElement.C_TEMPLATE_UNION : ICElement.C_UNION;
           break;
         case ICPPASTCompositeTypeSpecifier.k_class:
           nodeType = isTemplateDecl ? ICElement.C_TEMPLATE_CLASS : ICElement.C_CLASS;
           break;
         default:
           assert false : "Unexpected composite type specifier"; // $NON-NLS-1$
           return PROCESS_CONTINUE;
       }
       assert getCurrentContainer().getTypeCode() == nodeType;
       pop(isTemplateDecl ? endOffset : getEndOffset(declSpec));
     } else if (declSpec instanceof IASTEnumerationSpecifier) {
       isCompositeType = true;
       assert getCurrentContainer().getTypeCode() == ICElement.C_ENUMERATION;
       pop(getEndOffset(declSpec));
     }
     if (isCompositeType) {
       IASTDeclarator[] declarators = simpleDecl.getDeclarators();
       for (int i = 0; i < declarators.length; i++) {
         IASTDeclarator declarator = declarators[i];
         final String nodeName = getDeclaratorName(declarator);
         final int declStartOffset = getStartOffset(declarator);
         final int declEndOffset = getEndOffset(declarator);
         if (declSpec.getStorageClass() == IASTDeclSpecifier.sc_typedef) {
           push(ICElement.C_TYPEDEF, nodeName, declStartOffset);
           pop(declEndOffset);
         } else if (declarator instanceof IASTFunctionDeclarator
             && !hasNestedPointerOperators(declarator)) {
           final int nodeType;
           if (inClassBody()) {
             nodeType =
                 isTemplateDecl
                     ? ICElement.C_TEMPLATE_METHOD_DECLARATION
                     : ICElement.C_METHOD_DECLARATION;
           } else {
             nodeType =
                 isTemplateDecl
                     ? ICElement.C_TEMPLATE_FUNCTION_DECLARATION
                     : ICElement.C_FUNCTION_DECLARATION;
           }
           push(nodeType, nodeName, declStartOffset);
           pop(declEndOffset);
         } else if (declarator != null) {
           final int nodeType;
           if (inClassBody()) {
             nodeType = ICElement.C_FIELD;
           } else {
             if (declSpec.getStorageClass() == IASTDeclSpecifier.sc_extern) {
               nodeType = ICElement.C_VARIABLE_DECLARATION;
             } else {
               nodeType = isTemplateDecl ? ICElement.C_TEMPLATE_VARIABLE : ICElement.C_VARIABLE;
             }
           }
           push(nodeType, nodeName, declStartOffset);
           pop(declEndOffset);
         }
       }
     }
   } else if (node instanceof IASTASMDeclaration) {
     // ignored
   } else if (node instanceof ICPPASTVisibilityLabel) {
     // ignored
   } else if (node instanceof ICPPASTNamespaceDefinition) {
     // handled below
   } else if (node instanceof ICPPASTNamespaceAlias) {
     // ignored
   } else if (node instanceof ICPPASTUsingDeclaration) {
     // handled in visit
   } else if (node instanceof ICPPASTUsingDirective) {
     // handled in visit
   } else if (node instanceof ICPPASTLinkageSpecification) {
     // declarations get flattened
   } else if (node instanceof ICPPASTTemplateDeclaration) {
     // handled at child declaration level
   } else if (node instanceof ICPPASTTemplateSpecialization) {
     // ignored
   } else if (node instanceof ICPPASTExplicitTemplateInstantiation) {
     // ignored
   } else if (node instanceof IASTProblemDeclaration) {
     // ignored
   }
   return PROCESS_CONTINUE;
 }