Beispiel #1
0
 public String getReturnType() {
   if (!isReturnValue()) return null;
   INodeFactory nodeFactory = name.getTranslationUnit().getASTNodeFactory();
   IASTDeclarator sourceDeclarator = getDeclarator();
   IASTDeclSpecifier declSpec = safeCopy(getDeclSpecifier());
   IASTDeclarator declarator = createDeclarator(nodeFactory, sourceDeclarator, null);
   IASTParameterDeclaration declaration =
       nodeFactory.newParameterDeclaration(declSpec, declarator);
   ASTWriterVisitor writer = new ASTWriterVisitor();
   declaration.accept(writer);
   return writer.toString();
 }
Beispiel #2
0
  private IASTParameterDeclaration getParameterDeclaration(
      INodeFactory nodeFactory, String paramName) {
    IASTDeclarator sourceDeclarator = getDeclarator();
    IASTDeclSpecifier declSpec = safeCopy(getDeclSpecifier());
    IASTDeclarator declarator = createDeclarator(nodeFactory, sourceDeclarator, paramName);

    Indirection indirection = getIndirection();
    if (indirection == Indirection.POINTER) {
      declarator.addPointerOperator(nodeFactory.newPointer());
    } else if (indirection == Indirection.REFERENCE) {
      declarator.addPointerOperator(((ICPPNodeFactory) nodeFactory).newReferenceOperator(false));
    }

    if (indirection != Indirection.NONE && !isWriteAccess && declSpec != null) {
      declSpec.setConst(true);
    }

    declarator.setNestedDeclarator(sourceDeclarator.getNestedDeclarator());
    return nodeFactory.newParameterDeclaration(declSpec, declarator);
  }
Beispiel #3
0
 private IASTDeclarator createDeclarator(
     INodeFactory nodeFactory, IASTDeclarator sourceDeclarator, String name) {
   IASTName astName =
       name != null ? nodeFactory.newName(name.toCharArray()) : nodeFactory.newName();
   IASTDeclarator declarator;
   if (sourceDeclarator instanceof IASTArrayDeclarator) {
     IASTArrayDeclarator arrDeclarator = (IASTArrayDeclarator) sourceDeclarator;
     IASTArrayDeclarator arrayDeclarator = nodeFactory.newArrayDeclarator(astName);
     IASTArrayModifier[] arrayModifiers = arrDeclarator.getArrayModifiers();
     for (IASTArrayModifier arrayModifier : arrayModifiers) {
       arrayDeclarator.addArrayModifier(arrayModifier.copy(CopyStyle.withLocations));
     }
     declarator = arrayDeclarator;
   } else {
     declarator = nodeFactory.newDeclarator(astName);
   }
   for (IASTPointerOperator pointerOp : sourceDeclarator.getPointerOperators()) {
     declarator.addPointerOperator(pointerOp.copy(CopyStyle.withLocations));
   }
   return declarator;
 }
Beispiel #4
0
  IASTStandardFunctionDeclarator createFunctionDeclarator(
      IASTName name,
      IASTStandardFunctionDeclarator functionDeclarator,
      NameInformation returnVariable,
      List<IASTNode> nodesToWrite,
      Collection<NameInformation> allUsedNames,
      INodeFactory nodeFactory) {
    IASTStandardFunctionDeclarator declarator = nodeFactory.newFunctionDeclarator(name);

    if (functionDeclarator instanceof ICPPASTFunctionDeclarator
        && declarator instanceof ICPPASTFunctionDeclarator) {
      if (((ICPPASTFunctionDeclarator) functionDeclarator).isConst()) {
        ((ICPPASTFunctionDeclarator) declarator).setConst(true);
      }
    }

    for (IASTParameterDeclaration param : getParameterDeclarations(allUsedNames, nodeFactory)) {
      declarator.addParameterDeclaration(param);
    }

    return declarator;
  }
 @Override
 public void modifyAST(IIndex index, IMarker marker) {
   CompositeChange c = new CompositeChange(Messages.QuickFixCreateParameter_0);
   try {
     ITranslationUnit baseTU = getTranslationUnitViaEditor(marker);
     IASTTranslationUnit baseAST = baseTU.getAST(index, ITranslationUnit.AST_SKIP_INDEXED_HEADERS);
     IASTName astName = getASTNameFromMarker(marker, baseAST);
     if (astName == null) {
       return;
     }
     IASTDeclaration declaration =
         CxxAstUtils.createDeclaration(astName, baseAST.getASTNodeFactory(), index);
     // We'll need a FunctionParameterDeclaration later
     final IASTDeclSpecifier finalDeclSpec = (IASTDeclSpecifier) declaration.getChildren()[0];
     final IASTDeclarator finalDeclarator = (IASTDeclarator) declaration.getChildren()[1];
     IASTFunctionDefinition function = CxxAstUtils.getEnclosingFunction(astName);
     if (function == null) {
       return;
     }
     // Find the function declarations
     NameFinderVisitor nameFinderVisitor = new NameFinderVisitor();
     function.accept(nameFinderVisitor);
     IASTName funcName = nameFinderVisitor.name;
     IBinding binding = funcName.resolveBinding();
     IIndexName[] declarations = index.findNames(binding, IIndex.FIND_DECLARATIONS_DEFINITIONS);
     if (declarations.length == 0) {
       return;
     }
     HashMap<ITranslationUnit, IASTTranslationUnit> cachedASTs =
         new HashMap<ITranslationUnit, IASTTranslationUnit>();
     HashMap<ITranslationUnit, ASTRewrite> cachedRewrites =
         new HashMap<ITranslationUnit, ASTRewrite>();
     for (IIndexName iname : declarations) {
       ITranslationUnit declTU = CxxAstUtils.getTranslationUnitFromIndexName(iname);
       if (declTU == null) {
         continue;
       }
       ASTRewrite rewrite;
       IASTTranslationUnit declAST;
       if (!cachedASTs.containsKey(declTU)) {
         declAST = declTU.getAST(index, ITranslationUnit.AST_SKIP_INDEXED_HEADERS);
         rewrite = ASTRewrite.create(declAST);
         cachedASTs.put(declTU, declAST);
         cachedRewrites.put(declTU, rewrite);
       } else {
         declAST = cachedASTs.get(declTU);
         rewrite = cachedRewrites.get(declTU);
       }
       IASTFileLocation fileLocation = iname.getFileLocation();
       IASTName declName =
           declAST
               .getNodeSelector(null)
               .findEnclosingName(fileLocation.getNodeOffset(), fileLocation.getNodeLength());
       if (declName == null) {
         continue;
       }
       INodeFactory factory = declAST.getASTNodeFactory();
       IASTFunctionDeclarator functionDecl;
       {
         IASTNode n = declName;
         while (n instanceof IASTName) {
           n = n.getParent();
         }
         functionDecl = (IASTFunctionDeclarator) n;
       }
       IASTParameterDeclaration newParam =
           factory.newParameterDeclaration(finalDeclSpec, finalDeclarator);
       rewrite.insertBefore(functionDecl, null, newParam, null);
     }
     for (ASTRewrite rewrite : cachedRewrites.values()) {
       c.add(rewrite.rewriteAST());
     }
     c.perform(new NullProgressMonitor());
   } catch (CoreException e) {
     CheckersUiActivator.log(e);
   }
 }