Exemplo n.º 1
0
  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$
    }
  }
Exemplo n.º 2
0
 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$
   }
 }
Exemplo n.º 3
0
 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$
   }
 }
Exemplo n.º 4
0
  public void testObjectMacroExpansionComplex() 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("#define IT int\n"); // $NON-NLS-1$
    buffer.append("#define V var\n"); // $NON-NLS-1$
    buffer.append("XYZ IT C_PO C_PO V;"); // $NON-NLS-1$
    String code = buffer.toString();

    for (ParserLanguage language : languages) {
      IASTTranslationUnit tu = parse(code, language);
      IASTPreprocessorObjectStyleMacroDefinition XYZ =
          (IASTPreprocessorObjectStyleMacroDefinition) tu.getMacroDefinitions()[0];
      //            IASTPreprocessorObjectStyleMacroDefinition PO =
      // (IASTPreprocessorObjectStyleMacroDefinition) tu.getMacroDefinitions()[1];
      IASTPreprocessorObjectStyleMacroDefinition C_PO =
          (IASTPreprocessorObjectStyleMacroDefinition) tu.getMacroDefinitions()[2];
      IASTPreprocessorObjectStyleMacroDefinition IT =
          (IASTPreprocessorObjectStyleMacroDefinition) tu.getMacroDefinitions()[3];
      IASTPreprocessorObjectStyleMacroDefinition V =
          (IASTPreprocessorObjectStyleMacroDefinition) tu.getMacroDefinitions()[4];

      IASTSimpleDeclaration var = (IASTSimpleDeclaration) tu.getDeclarations()[0];
      final IASTNodeLocation[] nodeLocations = var.getNodeLocations();

      assertEquals(10, nodeLocations.length);
      IASTMacroExpansionLocation first_loc = (IASTMacroExpansionLocation) nodeLocations[0];
      assertEqualsMacros(first_loc.getExpansion().getMacroDefinition(), XYZ);
      IASTFileLocation second_loc = (IASTFileLocation) nodeLocations[1];
      assertEquals(1, second_loc.getNodeLength());
      IASTMacroExpansionLocation third_loc = (IASTMacroExpansionLocation) nodeLocations[2];
      assertEqualsMacros(third_loc.getExpansion().getMacroDefinition(), IT);
      IASTFileLocation fourth_loc = (IASTFileLocation) nodeLocations[3];
      assertEquals(1, fourth_loc.getNodeLength());
      IASTMacroExpansionLocation fifth_loc = (IASTMacroExpansionLocation) nodeLocations[4];
      assertEqualsMacros(fifth_loc.getExpansion().getMacroDefinition(), C_PO);
      IASTFileLocation sixth_loc = (IASTFileLocation) nodeLocations[5];
      assertEquals(1, sixth_loc.getNodeLength());
      IASTMacroExpansionLocation seventh_loc = (IASTMacroExpansionLocation) nodeLocations[6];
      assertEqualsMacros(seventh_loc.getExpansion().getMacroDefinition(), C_PO);
      IASTFileLocation eighth_loc = (IASTFileLocation) nodeLocations[7];
      assertEquals(1, eighth_loc.getNodeLength());
      IASTMacroExpansionLocation ninth_loc = (IASTMacroExpansionLocation) nodeLocations[8];
      assertEqualsMacros(ninth_loc.getExpansion().getMacroDefinition(), V);
      IASTFileLocation tenth_loc = (IASTFileLocation) nodeLocations[9];
      assertEquals(1, tenth_loc.getNodeLength());

      final IASTFileLocation flatLocation = var.getFileLocation();
      assertNotNull(flatLocation);
      assertEquals(
          code.indexOf("XYZ IT C_PO C_PO V;"), flatLocation.getNodeOffset()); // $NON-NLS-1$
      assertEquals("XYZ IT C_PO C_PO V;".length(), flatLocation.getNodeLength()); // $NON-NLS-1$
    }
  }
Exemplo n.º 5
0
 private void assertMacroLocation(IASTDeclaration decl, int index, int length) {
   IASTSimpleDeclaration var = (IASTSimpleDeclaration) decl;
   IASTEqualsInitializer initializer =
       (IASTEqualsInitializer) var.getDeclarators()[0].getInitializer();
   IASTInitializerClause expr = initializer.getInitializerClause();
   assertNotNull(expr.getFileLocation());
   IASTNodeLocation[] locations = expr.getNodeLocations();
   assertEquals(1, locations.length);
   IASTMacroExpansionLocation macroExpansion = (IASTMacroExpansionLocation) locations[0];
   IASTNodeLocation[] expLocations = macroExpansion.getExpansion().getNodeLocations();
   assertEquals(1, expLocations.length);
   IASTFileLocation fileLocation = expLocations[0].asFileLocation();
   assertEquals(index, fileLocation.getNodeOffset());
   assertEquals(length, fileLocation.getNodeLength());
 }
Exemplo n.º 6
0
  public void testStdioBug() throws ParserException {
    StringBuffer buffer = new StringBuffer("#define    _PTR        void *\n"); // $NON-NLS-1$
    buffer.append("#define __cdecl __attribute__ ((__cdecl__))\n"); // $NON-NLS-1$
    buffer.append("#define _EXFUN(name, proto)     __cdecl name proto\n"); // $NON-NLS-1$
    buffer.append("_PTR     _EXFUN(memchr,(const _PTR, int, size_t));\n"); // $NON-NLS-1$
    String code = buffer.toString();

    for (ParserLanguage language : languages) {
      IASTTranslationUnit tu = parse(code, language, true, true);
      final IASTPreprocessorMacroDefinition[] macroDefinitions = tu.getMacroDefinitions();
      IASTPreprocessorObjectStyleMacroDefinition _PTR =
          (IASTPreprocessorObjectStyleMacroDefinition) macroDefinitions[0];
      IASTPreprocessorFunctionStyleMacroDefinition _EXFUN =
          (IASTPreprocessorFunctionStyleMacroDefinition) macroDefinitions[2];
      IASTSimpleDeclaration memchr = (IASTSimpleDeclaration) tu.getDeclarations()[0];
      IASTNodeLocation[] locations = memchr.getNodeLocations();
      assertEquals(locations.length, 4);
      IASTMacroExpansionLocation loc_1 = (IASTMacroExpansionLocation) locations[0];
      assertEqualsMacros(_PTR, loc_1.getExpansion().getMacroDefinition());
      IASTFileLocation loc_2 = (IASTFileLocation) locations[1];
      assertEquals(loc_2.getNodeOffset(), code.indexOf("     _EXFUN(")); // $NON-NLS-1$
      assertEquals(loc_2.getNodeLength(), "     ".length()); // $NON-NLS-1$
      IASTMacroExpansionLocation loc_3 = (IASTMacroExpansionLocation) locations[2];
      assertEqualsMacros(_EXFUN, loc_3.getExpansion().getMacroDefinition());
      IASTFileLocation loc_4 = (IASTFileLocation) locations[3];
      assertEquals(loc_4.getNodeOffset(), code.indexOf(";")); // $NON-NLS-1$
      assertEquals(loc_4.getNodeLength(), 1);
      IASTFileLocation flat = memchr.getFileLocation();
      assertEquals(
          flat.getNodeOffset(),
          code.indexOf("_PTR     _EXFUN(memchr,(const _PTR, int, size_t));")); // $NON-NLS-1$
      assertEquals(
          flat.getNodeLength(),
          "_PTR     _EXFUN(memchr,(const _PTR, int, size_t));".length()); // $NON-NLS-1$

      IASTDeclarator d = memchr.getDeclarators()[0];
      IASTFileLocation f = d.getFileLocation();
      assertEquals(
          code.indexOf("_PTR     _EXFUN(memchr,(const _PTR, int, size_t))"),
          f.getNodeOffset()); // $NON-NLS-1$
      assertEquals(
          "_PTR     _EXFUN(memchr,(const _PTR, int, size_t))".length(),
          f.getNodeLength()); // $NON-NLS-1$
    }
  }
Exemplo n.º 7
0
  public void testFunctionMacroExpansionWithNameSubstitution_Bug173637() throws Exception {
    StringBuffer buffer = new StringBuffer("#define PLUS5(x) (x+5)\n"); // $NON-NLS-1$
    buffer.append("#define FUNCTION PLUS5 \n"); // $NON-NLS-1$
    buffer.append("int var= FUNCTION(1);"); // $NON-NLS-1$
    String code = buffer.toString();

    for (ParserLanguage language : languages) {
      IASTTranslationUnit tu = parse(code, language);
      IASTSimpleDeclaration var = (IASTSimpleDeclaration) tu.getDeclarations()[0];
      IASTEqualsInitializer initializer =
          (IASTEqualsInitializer) var.getDeclarators()[0].getInitializer();
      IASTInitializerClause expr = initializer.getInitializerClause();
      assertNotNull(expr.getFileLocation());
      IASTNodeLocation[] locations = expr.getNodeLocations();
      assertEquals(1, locations.length);
      IASTMacroExpansionLocation macroExpansion = (IASTMacroExpansionLocation) locations[0];
      IASTNodeLocation[] expLocations = macroExpansion.getExpansion().getNodeLocations();
      assertEquals(1, expLocations.length);
      assertEquals(code.indexOf("FUNCTION(1)"), expLocations[0].getNodeOffset());
      assertEquals("FUNCTION(1)".length(), expLocations[0].getNodeLength());
    }
  }