private IName[] findDefinitions(
      IIndex index, IASTTranslationUnit ast, NameKind kind, IBinding binding) throws CoreException {
    List<IASTName> declNames = new ArrayList<IASTName>();
    declNames.addAll(Arrays.asList(ast.getDefinitionsInAST(binding)));
    for (Iterator<IASTName> i = declNames.iterator(); i.hasNext(); ) {
      IASTName name = i.next();
      final IBinding b2 = name.resolveBinding();
      if (b2 instanceof ICPPUsingDeclaration) {
        i.remove();
      }
      if (binding != b2 && binding instanceof ICPPSpecialization) {
        // Make sure binding specializes b2 so that for instance we do not navigate from
        // one partial specialization to another.
        IBinding spec = binding;
        while (spec instanceof ICPPSpecialization) {
          spec = ((ICPPSpecialization) spec).getSpecializedBinding();
          if (spec == b2) break;
        }
        if (!(spec instanceof ICPPSpecialization)) {
          i.remove();
        }
      }
    }
    if (!declNames.isEmpty()) {
      return declNames.toArray(new IASTName[declNames.size()]);
    }

    // 2. Try definition in index.
    return index.findNames(
        binding, IIndex.FIND_DEFINITIONS | IIndex.SEARCH_ACROSS_LANGUAGE_BOUNDARIES);
  }
示例#2
0
  public void testGCC20000225() throws Exception {
    StringBuilder buffer = new StringBuilder();
    buffer.append("int main() {                        \n");
    buffer.append("   int nResult, b = 0, i = -1;      \n");
    buffer.append("   do {                             \n");
    buffer.append("      if (b != 0) {                 \n");
    buffer.append("         nResult = 1;               \n");
    buffer.append("      } else {                      \n");
    buffer.append("         nResult = 0;               \n");
    buffer.append("      }                             \n");
    buffer.append("      i++;                          \n");
    buffer.append("      b = (i + 2) * 4;              \n");
    buffer.append("   } while (i < 0);                 \n");
    buffer.append("   return -1;                       \n");
    buffer.append("}                                   \n");

    IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.C);
    CNameCollector collector = new CNameCollector();
    tu.accept(collector);

    assertEquals(collector.size(), 11);
    IVariable nResult = (IVariable) collector.getName(1).resolveBinding();
    IVariable b = (IVariable) collector.getName(2).resolveBinding();
    IVariable i = (IVariable) collector.getName(3).resolveBinding();

    assertInstances(collector, nResult, 3);
    assertInstances(collector, b, 3);
    assertInstances(collector, i, 4);
  }
示例#3
0
  private IASTNode getNextSiblingOrPreprocessorNode(IASTNode node) {
    int endOffset = endOffset(node);
    IASTTranslationUnit ast = node.getTranslationUnit();
    IASTPreprocessorStatement[] preprocessorStatements = ast.getAllPreprocessorStatements();
    int low = 0;
    int high = preprocessorStatements.length;
    while (low < high) {
      int mid = (low + high) / 2;
      IASTNode statement = preprocessorStatements[mid];
      if (statement.isPartOfTranslationUnitFile() && offset(statement) > endOffset) {
        high = mid;
      } else {
        low = mid + 1;
      }
    }
    if (high < preprocessorStatements.length) {
      IASTNode statement = preprocessorStatements[high];
      if (statement.isPartOfTranslationUnitFile()) {
        int offset = offset(statement);
        if (!doesRegionContainNode(ast, endOffset, offset - endOffset)) {
          return statement;
        }
      }
    }

    return getNextSiblingNode(node);
  }
 public void testObjectStyleMacroExpansionSimpleDeclarator() throws Exception {
   StringBuffer buffer = new StringBuffer("#define ABC D\n"); // $NON-NLS-1$
   buffer.append("int ABC;"); // $NON-NLS-1$
   String code = buffer.toString();
   for (ParserLanguage language : languages) {
     IASTTranslationUnit tu = parse(code, language);
     IASTPreprocessorObjectStyleMacroDefinition ABC =
         (IASTPreprocessorObjectStyleMacroDefinition) tu.getMacroDefinitions()[0];
     IASTSimpleDeclaration var = (IASTSimpleDeclaration) tu.getDeclarations()[0];
     IASTDeclarator d = var.getDeclarators()[0];
     assertEquals(d.getName().toString(), "D"); // $NON-NLS-1$
     IASTNodeLocation[] declaratorLocations = d.getNodeLocations();
     assertEquals(declaratorLocations.length, 1);
     IASTMacroExpansionLocation expansion = (IASTMacroExpansionLocation) declaratorLocations[0];
     IASTPreprocessorObjectStyleMacroDefinition fromExpansion =
         (IASTPreprocessorObjectStyleMacroDefinition)
             expansion.getExpansion().getMacroDefinition();
     assertEqualsMacros(fromExpansion, ABC);
     assertEquals(expansion.getNodeOffset(), 0);
     assertEquals(expansion.getNodeLength(), 1);
     IASTNodeLocation[] macroLocation = expansion.getExpansion().getNodeLocations();
     assertEquals(macroLocation.length, 1);
     assertTrue(macroLocation[0] instanceof IASTFileLocation);
     assertEquals(
         macroLocation[0].getNodeOffset(),
         code.indexOf("int ABC;") + "int ".length()); // $NON-NLS-1$ //$NON-NLS-2$
     assertEquals(macroLocation[0].getNodeLength(), "ABC".length()); // $NON-NLS-1$
   }
 }
示例#5
0
  public void testGCC20000412_1() throws Exception {
    StringBuilder buffer = new StringBuilder();
    buffer.append("short int i = -1;                                               \n");
    buffer.append("const char * const wordlist[207];                               \n");
    buffer.append("const char * const * foo(void) {                                \n");
    buffer.append("   register const char * const *wordptr = &wordlist[207u + i];  \n");
    buffer.append("   return wordptr;                                              \n");
    buffer.append("}                                                               \n");
    buffer.append("int main() {                                                    \n");
    buffer.append("   if (foo() != &wordlist[206])                                 \n");
    buffer.append("      return -1;                                                \n");
    buffer.append("   return 0;                                                    \n");
    buffer.append("}                                                               \n");

    IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.C);
    CNameCollector collector = new CNameCollector();
    tu.accept(collector);

    assertEquals(collector.size(), 11);
    IVariable i = (IVariable) collector.getName(0).resolveBinding();
    IVariable wordlist = (IVariable) collector.getName(1).resolveBinding();
    IFunction foo = (IFunction) collector.getName(2).resolveBinding();
    IVariable wordptr = (IVariable) collector.getName(4).resolveBinding();

    assertInstances(collector, i, 2);
    assertInstances(collector, wordlist, 3);
    assertInstances(collector, foo, 2);
    assertInstances(collector, wordptr, 2);
  }
示例#6
0
  public void testGCC20000412_5() throws Exception {
    StringBuilder buffer = new StringBuilder();
    buffer.append("int main(void) {                                        \n");
    buffer.append("   struct {                                             \n");
    buffer.append("      int node;                                         \n");
    buffer.append("      int type;                                         \n");
    buffer.append("   } lastglob[1] = { { 0, 1 } };                        \n");
    buffer.append("   if (lastglob[0].node != 0 || lastglob[0].type != 1)  \n");
    buffer.append("      return -1;                                        \n");
    buffer.append("   return 0;                                            \n");
    buffer.append("}                                                       \n");

    IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.C);
    CNameCollector collector = new CNameCollector();
    tu.accept(collector);

    assertEquals(collector.size(), 10);
    IField node = (IField) collector.getName(3).resolveBinding();
    IField type = (IField) collector.getName(4).resolveBinding();
    IVariable lastglob = (IVariable) collector.getName(5).resolveBinding();

    assertInstances(collector, node, 2);
    assertInstances(collector, type, 2);
    assertInstances(collector, lastglob, 3);
  }
示例#7
0
  public void testGCC20000227() throws Exception {
    StringBuilder buffer = new StringBuilder();
    buffer.append("static const unsigned char f[] = \"\\0\\377\";        \n");
    buffer.append("static const unsigned char g[] = \"\\0y\";            \n");
    buffer.append("int main() {                                          \n");
    buffer.append("   if (sizeof f != 3 || sizeof g != 3)                \n");
    buffer.append("      return -1;                                      \n");
    buffer.append("   if (f[0] != g[0])                                  \n");
    buffer.append("      return -1;                                      \n");
    buffer.append("   if (f[1] != g[1] || f[2] != g[2])                  \n");
    buffer.append("      return -1;                                      \n");
    buffer.append("   return 0;                                          \n");
    buffer.append("}                                                     \n");

    IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.C);
    CNameCollector collector = new CNameCollector();
    tu.accept(collector);

    assertEquals(collector.size(), 11);
    IVariable f = (IVariable) collector.getName(0).resolveBinding();
    IVariable g = (IVariable) collector.getName(1).resolveBinding();

    assertInstances(collector, f, 5);
    assertInstances(collector, g, 5);
  }
示例#8
0
  public void testGCC20000314_2() throws Exception {
    StringBuilder buffer = new StringBuilder();
    buffer.append("typedef unsigned long long uint64;                \n");
    buffer.append("const uint64 bigconst = 1ULL << 34;               \n");
    buffer.append("int a = 1;                                        \n");
    buffer.append("static uint64 getmask(void) {                     \n");
    buffer.append("   if (a)  return bigconst;                       \n");
    buffer.append("   else   return 0;                               \n");
    buffer.append("}                                                 \n");
    buffer.append("main() {                                          \n");
    buffer.append("   uint64 f = getmask();                          \n");
    buffer.append("   if (sizeof (long long) == 8 && f != bigconst)  \n");
    buffer.append("      return -1;                                  \n");
    buffer.append("   return 0;                                      \n");
    buffer.append("}                                                 \n");

    IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.C);
    CNameCollector collector = new CNameCollector();
    tu.accept(collector);

    assertEquals(collector.size(), 15);
    ITypedef uint64 = (ITypedef) collector.getName(0).resolveBinding();
    IVariable bigconst = (IVariable) collector.getName(2).resolveBinding();
    IVariable a = (IVariable) collector.getName(3).resolveBinding();
    IFunction getmask = (IFunction) collector.getName(5).resolveBinding();
    IVariable f = (IVariable) collector.getName(11).resolveBinding();

    assertInstances(collector, uint64, 4);
    assertInstances(collector, bigconst, 3);
    assertInstances(collector, a, 2);
    assertInstances(collector, getmask, 2);
    assertInstances(collector, f, 2);
  }
示例#9
0
  public void testGCC20000217() throws Exception {
    StringBuilder buffer = new StringBuilder();
    buffer.append("unsigned short int showbug(unsigned short int * a,    \n");
    buffer.append("                            unsigned short int * b) { \n");
    buffer.append("   *a += *b - 8;                                      \n");
    buffer.append("   return (*a >= 8);                                  \n");
    buffer.append("}                                                     \n");
    buffer.append("int main() {                                          \n");
    buffer.append("   unsigned short int x = 0;                          \n");
    buffer.append("   unsigned short int y = 10;                         \n");
    buffer.append("   if (showbug(&x, &y) != 0)                          \n");
    buffer.append("      return -1;                                      \n");
    buffer.append("   return 0;                                          \n");
    buffer.append("}                                                     \n");

    IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.C);
    CNameCollector collector = new CNameCollector();
    tu.accept(collector);

    assertEquals(collector.size(), 12);

    IFunction showBug = (IFunction) collector.getName(0).resolveBinding();
    IVariable a = (IVariable) collector.getName(1).resolveBinding();
    IVariable b = (IVariable) collector.getName(2).resolveBinding();
    IVariable x = (IVariable) collector.getName(7).resolveBinding();
    IVariable y = (IVariable) collector.getName(8).resolveBinding();

    assertInstances(collector, showBug, 2);
    assertInstances(collector, a, 3);
    assertInstances(collector, b, 2);
    assertInstances(collector, x, 2);
    assertInstances(collector, y, 2);
  }
  /*
   * @see org.eclipse.cdt.core.dom.ast.ASTVisitor#visit(org.eclipse.cdt.core.dom.ast.IASTTranslationUnit)
   */
  @Override
  public int visit(IASTTranslationUnit tu) {
    fTranslationUnitFileName = tu.getFilePath();

    push(ICElement.C_UNIT, TRANSLATION_UNIT_NAME, 0);

    // TODO fix ordering of includes and macros
    // includes
    final IASTPreprocessorIncludeStatement[] includeDirectives = tu.getIncludeDirectives();
    for (int i = 0; i < includeDirectives.length; i++) {
      IASTPreprocessorIncludeStatement includeDirective = includeDirectives[i];
      if (isLocalToFile(includeDirective)) {
        push(
            ICElement.C_INCLUDE,
            new String(includeDirective.getName().toCharArray()),
            getStartOffset(includeDirective));
        pop(getEndOffset(includeDirective));
      }
    }
    // macros
    final IASTPreprocessorMacroDefinition[] macroDefinitions = tu.getMacroDefinitions();
    for (int i = 0; i < macroDefinitions.length; i++) {
      IASTPreprocessorMacroDefinition macroDefinition = macroDefinitions[i];
      if (isLocalToFile(macroDefinition)) {
        push(
            ICElement.C_MACRO,
            new String(macroDefinition.getName().toCharArray()),
            getStartOffset(macroDefinition));
        pop(getEndOffset(macroDefinition));
      }
    }

    return super.visit(tu);
  }
示例#11
0
  public void testGCCenum_2() throws Exception {
    StringBuilder buffer = new StringBuilder();
    buffer.append("enum foo { FOO, BAR };                                  \n");
    buffer.append("int main() {                                            \n");
    buffer.append("   int i;                                               \n");
    buffer.append("   for (i = BAR; i >= FOO; --i)                         \n");
    buffer.append("      if (i == -1) return -1;                           \n");
    buffer.append("   return 0;                                            \n");
    buffer.append("}                                                       \n");

    IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.C);
    CNameCollector collector = new CNameCollector();
    tu.accept(collector);

    assertEquals(collector.size(), 11);
    IEnumeration foo = (IEnumeration) collector.getName(0).resolveBinding();
    IEnumerator FOO = (IEnumerator) collector.getName(1).resolveBinding();
    IEnumerator BAR = (IEnumerator) collector.getName(2).resolveBinding();
    IVariable i = (IVariable) collector.getName(4).resolveBinding();

    assertInstances(collector, foo, 1);
    assertInstances(collector, FOO, 2);
    assertInstances(collector, BAR, 2);
    assertInstances(collector, i, 5);
  }
  public void testObjectMacroExpansionNested() throws Exception {
    StringBuffer buffer = new StringBuffer("#define XYZ const\n"); // $NON-NLS-1$
    buffer.append("#define PO *\n"); // $NON-NLS-1$
    buffer.append("#define C_PO PO XYZ\n"); // $NON-NLS-1$
    buffer.append("int C_PO var;"); // $NON-NLS-1$
    String code = buffer.toString();

    for (ParserLanguage language : languages) {
      IASTTranslationUnit tu = parse(code, language);
      final IASTPreprocessorMacroDefinition[] macroDefinitions = tu.getMacroDefinitions();
      IASTPreprocessorMacroDefinition XYZ = macroDefinitions[0];
      IASTPreprocessorMacroDefinition PO = macroDefinitions[1];
      IASTPreprocessorMacroDefinition C_PO = macroDefinitions[2];
      IASTSimpleDeclaration var = (IASTSimpleDeclaration) tu.getDeclarations()[0];
      assertTrue(var.getDeclarators()[0].getPointerOperators().length > 0);
      IASTNodeLocation[] locations = var.getNodeLocations();
      assertEquals(3, locations.length);
      IASTFileLocation start_loc = (IASTFileLocation) locations[0];
      assertEquals(start_loc.getNodeOffset(), code.indexOf("int")); // $NON-NLS-1$
      assertEquals(start_loc.getNodeLength(), "int ".length()); // $NON-NLS-1$
      IASTMacroExpansionLocation mac_loc = (IASTMacroExpansionLocation) locations[1];
      final IASTPreprocessorMacroDefinition C_PO2 = mac_loc.getExpansion().getMacroDefinition();
      assertEqualsMacros(C_PO, C_PO2);
      assertEquals(0, mac_loc.getNodeOffset());
      assertEquals(2, mac_loc.getNodeLength());
      IASTFileLocation end_loc = (IASTFileLocation) locations[2];
      assertEquals(code.indexOf(" var"), end_loc.getNodeOffset()); // $NON-NLS-1$
      assertEquals(" var;".length(), end_loc.getNodeLength()); // $NON-NLS-1$
    }
  }
示例#13
0
  public void testGCC20000412_2() throws Exception {
    StringBuilder buffer = new StringBuilder();
    buffer.append("int f(int a, int *y) {                   \n");
    buffer.append("   int x = a;                            \n");
    buffer.append("   if (a == 0)  return *y;               \n");
    buffer.append("   return f(a-1, &x);                    \n");
    buffer.append("}                                        \n");
    buffer.append("int main(int argc, char** argv) {        \n");
    buffer.append("   if (f(100, (int *) 0) != 1)           \n");
    buffer.append("      return -1;                         \n");
    buffer.append("   return 0;                             \n");
    buffer.append("}                                        \n");

    IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.C);
    CNameCollector collector = new CNameCollector();
    tu.accept(collector);

    assertEquals(collector.size(), 14);
    IFunction f = (IFunction) collector.getName(0).resolveBinding();
    IParameter a = (IParameter) collector.getName(1).resolveBinding();
    IParameter y = (IParameter) collector.getName(2).resolveBinding();
    IVariable x = (IVariable) collector.getName(3).resolveBinding();

    assertInstances(collector, f, 3);
    assertInstances(collector, a, 4);
    assertInstances(collector, y, 2);
    assertInstances(collector, x, 2);
  }
 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$
   }
 }
示例#15
0
  public void testGCC20000412_4() throws Exception {
    StringBuilder buffer = new StringBuilder();
    buffer.append("void f(int i, int j, int radius, int width, int N) { \n");
    buffer.append("   const int diff = i - radius;                      \n");
    buffer.append("   const int lowk = (diff > 0 ? diff : 0);           \n");
    buffer.append("   int k;                                            \n");
    buffer.append("   for (k = lowk; k <= 2; k++) {                     \n");
    buffer.append("      int idx = ((k-i+radius) * width - j + radius); \n");
    buffer.append("      if (idx < 0) return -1;                        \n");
    buffer.append("   }                                                 \n");
    buffer.append("   for (k = lowk; k <= 2; k++) ;                     \n");
    buffer.append("}                                                    \n");
    buffer.append("int main (int argc, char** argv) {                   \n");
    buffer.append("   int exc_rad = 2;                                  \n");
    buffer.append("   int N = 8;                                        \n");
    buffer.append("   int i;                                            \n");
    buffer.append("   for (i = 1; i < 4; i++)                           \n");
    buffer.append("      f(i, 1, exc_rad, 2*exc_rad + 1, N);            \n");
    buffer.append("   return 0;                                         \n");
    buffer.append("}                                                    \n");

    IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.C);
    CNameCollector collector = new CNameCollector();
    tu.accept(collector);

    assertEquals(collector.size(), 43);
    IFunction f = (IFunction) collector.getName(0).resolveBinding();
    IParameter i1 = (IParameter) collector.getName(1).resolveBinding();
    IParameter j = (IParameter) collector.getName(2).resolveBinding();
    IParameter radius = (IParameter) collector.getName(3).resolveBinding();
    IParameter width = (IParameter) collector.getName(4).resolveBinding();
    IParameter N1 = (IParameter) collector.getName(5).resolveBinding();
    IVariable diff = (IVariable) collector.getName(6).resolveBinding();
    IVariable lowk = (IVariable) collector.getName(9).resolveBinding();
    IVariable k = (IVariable) collector.getName(12).resolveBinding();
    IVariable idx = (IVariable) collector.getName(17).resolveBinding();
    IVariable exc_rad = (IVariable) collector.getName(32).resolveBinding();
    IVariable N2 = (IVariable) collector.getName(33).resolveBinding();
    IVariable i2 = (IVariable) collector.getName(34).resolveBinding();

    assertInstances(collector, f, 2);
    assertInstances(collector, i1, 3);
    assertInstances(collector, j, 2);
    assertInstances(collector, radius, 4);
    assertInstances(collector, width, 2);
    assertInstances(collector, N1, 1);
    assertInstances(collector, diff, 3);
    assertInstances(collector, lowk, 3);
    assertInstances(collector, k, 8);
    assertInstances(collector, idx, 2);
    assertInstances(collector, exc_rad, 3);
    assertInstances(collector, N2, 2);
    assertInstances(collector, i2, 5);
  }
 public void testBug94933() throws Exception {
   StringBuffer buffer = new StringBuffer("#define API extern\n"); // $NON-NLS-1$
   buffer.append("#define MYAPI API\n"); // $NON-NLS-1$
   buffer.append("MYAPI void func() {}"); // $NON-NLS-1$
   String code = buffer.toString();
   for (ParserLanguage language : languages) {
     IASTTranslationUnit tu = parse(code, language);
     IASTFunctionDefinition f = (IASTFunctionDefinition) tu.getDeclarations()[0];
     assertNotNull(f.getFileLocation());
   }
 }
示例#17
0
  public void testGCC20000511() throws Exception {
    StringBuilder buffer = new StringBuilder();
    buffer.append("int f(int value, int expect) {                      \n");
    buffer.append("   return (value == expect);                        \n");
    buffer.append("}                                                   \n");
    buffer.append("main() {                                            \n");
    buffer.append("   int a = 7, b = 6, c = 4, d = 7, e = 2;           \n");
    buffer.append("   f(a || b % c, 1);                                \n");
    buffer.append("   f(a ? b % c : 0, 2);                             \n");
    buffer.append("   f(a = b % c, 2);                                 \n");
    buffer.append("   f(a *= b % c, 4);                                \n");
    buffer.append("   f(a /= b % c, 2);                                \n");
    buffer.append("   f(a %= b % c, 0);                                \n");
    buffer.append("   f(a += b % c, 2);                                \n");
    buffer.append("   f(d || c && e, 1);                               \n");
    buffer.append("   f(d ? c && e : 0, 1);                            \n");
    buffer.append("   f(d = c && e, 1);                                \n");
    buffer.append("   f(d *= c && e, 1);                               \n");
    buffer.append("   f(d %= c && e, 0);                               \n");
    buffer.append("   f(d += c && e, 1);                               \n");
    buffer.append("   f(d -= c && e, 0);                               \n");
    buffer.append("   f(d || c || e, 1);                               \n");
    buffer.append("   f(d ? c || e : 0, 0);                            \n");
    buffer.append("   f(d = c || e, 1);                                \n");
    buffer.append("   f(d *= c || e, 1);                               \n");
    buffer.append("   f(d %= c || e, 0);                               \n");
    buffer.append("   f(d += c || e, 1);                               \n");
    buffer.append("   f(d -= c || e, 0);                               \n");
    buffer.append("}                                                   \n");

    IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.C);
    CNameCollector collector = new CNameCollector();
    tu.accept(collector);

    assertEquals(collector.size(), 95);
    IFunction f = (IFunction) collector.getName(0).resolveBinding();
    IParameter v = (IParameter) collector.getName(1).resolveBinding();
    IParameter ex = (IParameter) collector.getName(2).resolveBinding();
    IVariable a = (IVariable) collector.getName(6).resolveBinding();
    IVariable b = (IVariable) collector.getName(7).resolveBinding();
    IVariable c = (IVariable) collector.getName(8).resolveBinding();
    IVariable d = (IVariable) collector.getName(9).resolveBinding();
    IVariable e = (IVariable) collector.getName(10).resolveBinding();

    assertInstances(collector, f, 22);
    assertInstances(collector, v, 2);
    assertInstances(collector, ex, 2);
    assertInstances(collector, a, 8);
    assertInstances(collector, b, 8);
    assertInstances(collector, c, 22);
    assertInstances(collector, d, 15);
    assertInstances(collector, e, 15);
  }
 protected final void checkBindings() throws Exception {
   for (int i = 0; i < strategy.getAstCount(); i++) {
     IASTTranslationUnit ast = strategy.getAst(i);
     CNameCollector col = new CNameCollector();
     ast.accept(col);
     for (IASTName n : col.nameList) {
       assertFalse(
           "ProblemBinding for " + n.getRawSignature(),
           n.resolveBinding() instanceof IProblemBinding);
     }
   }
 }
示例#19
0
 /** Checks if a given region contains at least a piece of a node after rewrite. */
 private boolean doesRegionContainNode(IASTTranslationUnit ast, int offset, int length) {
   IASTNodeSelector nodeSelector = ast.getNodeSelector(ast.getFilePath());
   while (length > 0) {
     IASTNode node = nodeSelector.findFirstContainedNode(offset, length - 1);
     if (node == null) return false;
     if (!isNodeRemoved(node)) return true;
     int oldOffset = offset;
     offset = endOffset(node);
     length -= offset - oldOffset;
   }
   return false;
 }
  protected IASTName findName(String section, int len) {
    if (len == 0) len = section.length();
    for (int i = 0; i < strategy.getAstCount(); i++) {
      IASTTranslationUnit ast = strategy.getAst(i);
      final IASTNodeSelector nodeSelector = ast.getNodeSelector(null);
      final int offset = strategy.getAstSource(i).indexOf(section);
      if (offset >= 0) {
        IASTName name = nodeSelector.findName(offset, len);
        if (name == null) name = nodeSelector.findImplicitName(offset, len);
        return name;
      }
    }

    return null;
  }
示例#21
0
  private IASTFunctionDeclarator findDeclaratorInSelection(
      ITextSelection selection, IASTTranslationUnit unit) {
    IASTNode firstNodeInsideSelection =
        unit.getNodeSelector(null)
            .findFirstContainedNode(selection.getOffset(), selection.getLength());
    IASTFunctionDeclarator declarator = findDeclaratorInAncestors(firstNodeInsideSelection);

    if (declarator == null) {
      firstNodeInsideSelection =
          unit.getNodeSelector(null)
              .findEnclosingNode(selection.getOffset(), selection.getLength());
      declarator = findDeclaratorInAncestors(firstNodeInsideSelection);
    }
    return declarator;
  }
示例#22
0
  public void testGCC20000419() throws Exception {
    StringBuilder buffer = new StringBuilder();
    buffer.append("struct foo { int a, b, c; };                          \n");
    buffer.append("void brother(int a, int b, int c) {                   \n");
    buffer.append("   if (a) return;                                     \n");
    buffer.append("}                                                     \n");
    buffer.append("void sister(struct foo f, int b, int c) {             \n");
    buffer.append("   brother((f.b == b), b, c);                         \n");
    buffer.append("}                                                     \n");
    buffer.append("int main() {                                          \n");
    buffer.append("   struct foo f = { 7, 8, 9 };                        \n");
    buffer.append("   sister(f, 1, 2);                                   \n");
    buffer.append("   return 0;                                          \n");
    buffer.append("}                                                     \n");

    IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.C);
    CNameCollector collector = new CNameCollector();
    tu.accept(collector);

    assertEquals(collector.size(), 25);
    ICompositeType foo = (ICompositeType) collector.getName(0).resolveBinding();
    IField fa = (IField) collector.getName(1).resolveBinding();
    IField fb = (IField) collector.getName(2).resolveBinding();
    IField fc = (IField) collector.getName(3).resolveBinding();
    IFunction brother = (IFunction) collector.getName(4).resolveBinding();
    IParameter pa = (IParameter) collector.getName(5).resolveBinding();
    IParameter pb = (IParameter) collector.getName(6).resolveBinding();
    IParameter pc = (IParameter) collector.getName(7).resolveBinding();
    IFunction sister = (IFunction) collector.getName(9).resolveBinding();
    IParameter sf = (IParameter) collector.getName(11).resolveBinding();
    IParameter sb = (IParameter) collector.getName(12).resolveBinding();
    IParameter sc = (IParameter) collector.getName(13).resolveBinding();
    IVariable f = (IVariable) collector.getName(22).resolveBinding();

    assertInstances(collector, foo, 3);
    assertInstances(collector, fa, 1);
    assertInstances(collector, fb, 2);
    assertInstances(collector, fc, 1);
    assertInstances(collector, brother, 2);
    assertInstances(collector, pa, 2);
    assertInstances(collector, pb, 1);
    assertInstances(collector, pc, 1);
    assertInstances(collector, sister, 2);
    assertInstances(collector, sf, 2);
    assertInstances(collector, sb, 3);
    assertInstances(collector, sc, 2);
    assertInstances(collector, f, 2);
  }
 private IASTCompositeTypeSpecifier findCurrentCompositeTypeSpecifier(IASTTranslationUnit ast)
     throws OperationCanceledException, CoreException {
   final int start = selectedRegion.getOffset();
   Container<IASTCompositeTypeSpecifier> container = new Container<IASTCompositeTypeSpecifier>();
   ast.accept(new CompositeTypeSpecFinder(start, container));
   return container.getObject();
 }
 private ILocationResolver getResolver(IASTTranslationUnit tu) {
   final ILocationResolver resolver = (ILocationResolver) tu.getAdapter(ILocationResolver.class);
   if (resolver == null) {
     throw new IllegalArgumentException();
   }
   return resolver;
 }
  public MultiMacroExpansionExplorer(IASTTranslationUnit tu, IASTFileLocation loc) {
    if (tu == null || loc == null || loc.getNodeLength() == 0) {
      throw new IllegalArgumentException();
    }
    final ILocationResolver resolver = getResolver(tu);
    final IASTNodeSelector nodeLocator = tu.getNodeSelector(null);
    final IASTPreprocessorMacroExpansion[] expansions = resolver.getMacroExpansions(loc);
    final int count = expansions.length;

    loc = extendLocation(loc, expansions);
    fMacroLocations = getMacroLocations(resolver);
    fFilePath = tu.getFilePath();
    fSource = resolver.getUnpreprocessedSignature(loc);
    fBoundaries = new int[count * 2 + 1];
    fDelegates = new SingleMacroExpansionExplorer[count];

    final int firstOffset = loc.getNodeOffset();
    int bidx = -1;
    int didx = -1;
    for (IASTPreprocessorMacroExpansion expansion : expansions) {
      IASTName ref = expansion.getMacroReference();
      if (ref != null) {
        ArrayList<IASTName> refs = new ArrayList<IASTName>();
        refs.add(ref);
        refs.addAll(Arrays.asList(expansion.getNestedMacroReferences()));
        IASTFileLocation refLoc = expansion.getFileLocation();
        int from = refLoc.getNodeOffset() - firstOffset;
        int to = from + refLoc.getNodeLength();
        IASTNode enclosing = nodeLocator.findEnclosingNode(from + firstOffset - 1, 2);
        boolean isPPCond =
            enclosing instanceof IASTPreprocessorIfStatement
                || enclosing instanceof IASTPreprocessorElifStatement;
        fBoundaries[++bidx] = from;
        fBoundaries[++bidx] = to;
        fDelegates[++didx] =
            new SingleMacroExpansionExplorer(
                new String(fSource, from, to - from),
                refs.toArray(new IASTName[refs.size()]),
                fMacroLocations,
                fFilePath,
                refLoc.getStartingLineNumber(),
                isPPCond,
                (LexerOptions) tu.getAdapter(LexerOptions.class));
      }
    }
    fBoundaries[++bidx] = fSource.length;
  }
示例#26
0
  public void testGCC20000403() throws Exception {
    StringBuilder buffer = new StringBuilder();
    buffer.append("extern unsigned long aa[], bb[];                                   \n");
    buffer.append("int seqgt(unsigned long a, unsigned short win, unsigned long b);   \n");
    buffer.append("int seqgt2 (unsigned long a, unsigned short win, unsigned long b); \n");
    buffer.append("main() {                                                           \n");
    buffer.append("   if (!seqgt(*aa, 0x1000, *bb) || !seqgt2(*aa, 0x1000, *bb))      \n");
    buffer.append("      return -1;                                                   \n");
    buffer.append("   return 0;                                                       \n");
    buffer.append("}                                                                  \n");
    buffer.append("int seqgt(unsigned long a, unsigned short win, unsigned long b) {  \n");
    buffer.append("   return (long) ((a + win) - b) > 0;                              \n");
    buffer.append("}                                                                  \n");
    buffer.append("int seqgt2(unsigned long a, unsigned short win, unsigned long b) { \n");
    buffer.append("   long l = ((a + win) - b);                                       \n");
    buffer.append("   return 1 > 0;                                                   \n");
    buffer.append("}                                                                  \n");
    buffer.append("unsigned long aa[] = { (1UL << (sizeof(long) *8 - 1)) = 0xfff };   \n");
    buffer.append("unsigned long bb[] = { (1UL << (sizeof(long) *8 - 1)) = 0xfff };   \n");

    IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.C);
    CNameCollector collector = new CNameCollector();
    tu.accept(collector);

    assertEquals(collector.size(), 34);
    IVariable aa = (IVariable) collector.getName(0).resolveBinding();
    IVariable bb = (IVariable) collector.getName(1).resolveBinding();
    IFunction seqgt = (IFunction) collector.getName(2).resolveBinding();
    IParameter a1 = (IParameter) collector.getName(3).resolveBinding();
    IParameter win1 = (IParameter) collector.getName(4).resolveBinding();
    IParameter b1 = (IParameter) collector.getName(5).resolveBinding();
    IFunction seqgt2 = (IFunction) collector.getName(6).resolveBinding();
    IParameter a2 = (IParameter) collector.getName(7).resolveBinding();
    IParameter win2 = (IParameter) collector.getName(8).resolveBinding();
    IParameter b2 = (IParameter) collector.getName(9).resolveBinding();

    assertInstances(collector, aa, 4);
    assertInstances(collector, bb, 4);
    assertInstances(collector, seqgt, 3);
    assertInstances(collector, a1, 3);
    assertInstances(collector, win1, 3);
    assertInstances(collector, b1, 3);
    assertInstances(collector, seqgt2, 3);
    assertInstances(collector, a2, 3);
    assertInstances(collector, win2, 3);
    assertInstances(collector, b2, 3);
  }
示例#27
0
  public void testGCC20000412_3() throws Exception {
    StringBuilder buffer = new StringBuilder();
    buffer.append("typedef struct {                         \n");
    buffer.append("   char y;                               \n");
    buffer.append("   char x[32];                           \n");
    buffer.append("} X;                                     \n");
    buffer.append("int z(void) {                            \n");
    buffer.append("   X xxx;                                \n");
    buffer.append("   xxx.x[0] = xxx.x[31] = '0';           \n");
    buffer.append("   xxx.y = 0xf;                          \n");
    buffer.append("   return f(xxx, xxx);                   \n");
    buffer.append("}                                        \n");
    buffer.append("int main (void) {                        \n");
    buffer.append("   int val;                              \n");
    buffer.append("   val = z();                            \n");
    buffer.append("   if (val != 0x60) return -1;           \n");
    buffer.append("   return 0;                             \n");
    buffer.append("}                                        \n");
    buffer.append("int f(X x, X y) {                        \n");
    buffer.append("   if (x.y != y.y)                       \n");
    buffer.append("      return 'F';                        \n");
    buffer.append("   return x.x[0] + y.x[0];               \n");
    buffer.append("}                                        \n");

    IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.C);
    CNameCollector collector = new CNameCollector();
    tu.accept(collector);

    assertEquals(collector.size(), 36);
    IField y = (IField) collector.getName(1).resolveBinding();
    IField x = (IField) collector.getName(2).resolveBinding();
    ITypedef X = (ITypedef) collector.getName(3).resolveBinding();
    IFunction z = (IFunction) collector.getName(4).resolveBinding();
    IVariable xxx = (IVariable) collector.getName(7).resolveBinding();
    IVariable val = (IVariable) collector.getName(19).resolveBinding();
    IParameter px = (IParameter) collector.getName(25).resolveBinding();
    IParameter py = (IParameter) collector.getName(27).resolveBinding();

    assertInstances(collector, y, 4);
    assertInstances(collector, x, 5);
    assertInstances(collector, X, 4);
    assertInstances(collector, z, 2);
    assertInstances(collector, xxx, 6);
    assertInstances(collector, val, 3);
    assertInstances(collector, px, 3);
    assertInstances(collector, py, 3);
  }
  public void testArgumentCapture() throws Exception {
    StringBuffer sb = new StringBuffer();
    sb.append("#define add(x,y) x + y \n"); // $NON-NLS-1$
    sb.append("#define add2 add(x,   \n"); // $NON-NLS-1$
    sb.append("int x = add2 z); \n"); // $NON-NLS-1$
    sb.append("int y = whatever; \n"); // $NON-NLS-1$
    String code = sb.toString();

    for (ParserLanguage language : languages) {
      IASTTranslationUnit tu = parse(code, language);
      IASTDeclaration[] decls = tu.getDeclarations();
      assertMacroLocation(
          decls[0], code.indexOf("add2 z);"), "add2 z)".length()); // $NON-NLS-1$ //$NON-NLS-2$
      assertExpressionLocation(
          decls[1], code.indexOf("whatever;"), "whatever".length()); // $NON-NLS-1$ //$NON-NLS-2$
    }
  }
  public void testFunctionMacroNotCalled() throws Exception {
    StringBuffer sb = new StringBuffer();
    sb.append("#define FUNCTION(x) x \n"); // $NON-NLS-1$
    sb.append("#define YO FUNCTION \n"); // $NON-NLS-1$
    sb.append("int x = YO; \n"); // $NON-NLS-1$
    sb.append("int y = whatever; \n"); // $NON-NLS-1$
    String code = sb.toString();

    for (ParserLanguage language : languages) {
      IASTTranslationUnit tu = parse(code, language);
      IASTDeclaration[] decls = tu.getDeclarations();
      assertMacroLocation(
          decls[0], code.indexOf("YO;"), "YO".length()); // $NON-NLS-1$ //$NON-NLS-2$
      assertExpressionLocation(
          decls[1], code.indexOf("whatever;"), "whatever".length()); // $NON-NLS-1$ //$NON-NLS-2$
    }
  }
示例#30
0
  private void handleAppends(IASTTranslationUnit tu) {
    List<ASTModification> modifications = getModifications(tu, ModificationKind.APPEND_CHILD);
    if (modifications.isEmpty()) return;

    IASTNode prevNode = null;
    IASTDeclaration[] declarations = tu.getDeclarations();
    if (declarations.length != 0) {
      prevNode = declarations[declarations.length - 1];
    } else {
      IASTPreprocessorStatement[] preprocessorStatements = tu.getAllPreprocessorStatements();
      if (preprocessorStatements.length != 0) {
        prevNode = preprocessorStatements[preprocessorStatements.length - 1];
      }
    }
    int offset = prevNode != null ? getEndOffsetIncludingComments(prevNode) : 0;
    String source = tu.getRawSignature();
    int endOffset = skipTrailingBlankLines(source, offset);

    ChangeGeneratorWriterVisitor writer =
        new ChangeGeneratorWriterVisitor(modificationStore, commentMap);
    IASTNode newNode = null;
    for (ASTModification modification : modifications) {
      boolean first = newNode == null;
      newNode = modification.getNewNode();
      if (first) {
        if (prevNode != null) {
          writer.newLine();
          if (ASTWriter.requireBlankLineInBetween(prevNode, newNode)) {
            writer.newLine();
          }
        }
      }
      newNode.accept(writer);
    }
    if (prevNode != null) {
      IASTNode nextNode = getNextSiblingOrPreprocessorNode(prevNode);
      if (nextNode != null && ASTWriter.requireBlankLineInBetween(newNode, nextNode)) {
        writer.newLine();
      }
    }

    String code = writer.toString();
    IFile file = FileHelper.getFileFromNode(tu);
    MultiTextEdit parentEdit = getEdit(tu, file);
    parentEdit.addChild(new ReplaceEdit(offset, endOffset - offset, code));
  }