Search in sources :

Example 1 with Annotation

use of de.fraunhofer.aisec.cpg.graph.Annotation in project cpg by Fraunhofer-AISEC.

the class CXXLanguageFrontendTest method testAttributes.

@Test
void testAttributes() throws Exception {
    File file = new File("src/test/resources/attributes.cpp");
    List<TranslationUnitDeclaration> declarations = TestUtils.analyzeWithBuilder(TranslationConfiguration.builder().sourceLocations(List.of(file)).topLevel(file.getParentFile()).defaultPasses().defaultLanguages().processAnnotations(true).symbols(Map.of("PROPERTY_ATTRIBUTE(...)", "[[property_attribute(#__VA_ARGS__)]]")));
    assertFalse(declarations.isEmpty());
    TranslationUnitDeclaration tu = declarations.get(0);
    assertNotNull(tu);
    FunctionDeclaration main = tu.getDeclarationsByName("main", FunctionDeclaration.class).iterator().next();
    assertNotNull(main);
    assertEquals("function_attribute", main.getAnnotations().get(0).getName());
    RecordDeclaration someClass = tu.getDeclarationsByName("SomeClass", RecordDeclaration.class).iterator().next();
    assertNotNull(someClass);
    assertEquals("record_attribute", someClass.getAnnotations().get(0).getName());
    FieldDeclaration a = someClass.getFields().stream().filter(f -> f.getName().equals("a")).findAny().orElse(null);
    assertNotNull(a);
    Annotation annotation = a.getAnnotations().get(0);
    assertNotNull(annotation);
    assertEquals("property_attribute", annotation.getName());
    assertEquals(3, annotation.getMembers().size());
    assertEquals("a", ((Literal<String>) annotation.getMembers().get(0).getValue()).getValue());
    FieldDeclaration b = someClass.getFields().stream().filter(f -> f.getName().equals("b")).findAny().orElse(null);
    assertNotNull(a);
    annotation = b.getAnnotations().get(0);
    assertNotNull(annotation);
    assertEquals("property_attribute", annotation.getName());
    assertEquals(1, annotation.getMembers().size());
    assertEquals("SomeCategory, SomeOtherThing", ((Literal<String>) annotation.getMembers().get(0).getValue()).getValue());
}
Also used : FunctionDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration) RecordDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration) File(java.io.File) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) FieldDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FieldDeclaration) Annotation(de.fraunhofer.aisec.cpg.graph.Annotation) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Aggregations

BaseTest (de.fraunhofer.aisec.cpg.BaseTest)1 Annotation (de.fraunhofer.aisec.cpg.graph.Annotation)1 FieldDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.FieldDeclaration)1 FunctionDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration)1 RecordDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration)1 TranslationUnitDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration)1 File (java.io.File)1 Test (org.junit.jupiter.api.Test)1