Search in sources :

Example 11 with OCFile

use of com.jetbrains.cidr.lang.psi.OCFile in project intellij by bazelbuild.

the class IwyuPragmasTest method noPragmas.

@Test
public void noPragmas() {
    OCFile file = createFile("bar.cc", "#include \"bar.h\"", "", "#include <memory>", "#include <vector>", "", "#include \"f/foo1.h\"", "#include \"f/foo2.h\"", "#include \"f/foo3.h\"  // blah", "", "void bar() {}");
    IwyuPragmas pragmas = IwyuPragmas.parse(file);
    assertThat(pragmas.privatePragma.isPresent()).isFalse();
    assertThat(pragmas.keeps).isEmpty();
    assertThat(pragmas.exports).isEmpty();
    assertThat(pragmas.associatedHeader.isPresent()).isFalse();
}
Also used : OCFile(com.jetbrains.cidr.lang.psi.OCFile) Test(org.junit.Test)

Example 12 with OCFile

use of com.jetbrains.cidr.lang.psi.OCFile in project intellij by bazelbuild.

the class DelegatingSwitchToHeaderOrSourceProvider method getItems.

@Override
public List<? extends GotoRelatedItem> getItems(DataContext context) {
    Project project = CommonDataKeys.PROJECT.getData(context);
    PsiFile psiFile = CommonDataKeys.PSI_FILE.getData(context);
    if (!(psiFile instanceof OCFile) || project == null || !Blaze.isBlazeProject(project)) {
        return ImmutableList.of();
    }
    Optional<List<? extends GotoRelatedItem>> fromDelegate = getItemsWithTimeout(() -> DELEGATE.getItems(context));
    if (!fromDelegate.isPresent()) {
        logger.info("Timed out: Trying fallback for .h <-> .cc");
        return getItemsFallback((OCFile) psiFile);
    }
    return fromDelegate.get();
}
Also used : OCFile(com.jetbrains.cidr.lang.psi.OCFile) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) GotoRelatedItem(com.intellij.navigation.GotoRelatedItem)

Example 13 with OCFile

use of com.jetbrains.cidr.lang.psi.OCFile in project intellij by bazelbuild.

the class GoogleTestLocation method findGoogleTest.

@Nullable
public static GoogleTestLocation findGoogleTest(PsiElement element) {
    // Copied from on CidrGoogleTestRunConfigurationProducer::findTestObject.
    // Precedence order (decreasing): class/function, macro, file
    PsiElement parent = PsiTreeUtil.getNonStrictParentOfType(element, OCFunctionDefinition.class, OCStruct.class);
    OCStructSymbol parentSymbol;
    if (parent instanceof OCStruct && ((parentSymbol = ((OCStruct) parent).getSymbol()) != null) && CidrGoogleTestUtil.isGoogleTestClass(parentSymbol)) {
        Couple<String> name = CidrGoogleTestUtil.extractGoogleTestName(parentSymbol);
        if (name != null) {
            return createFromClassAndMethod(parent, name.first, name.second);
        }
        String className = parentSymbol.getQualifiedName().getName();
        return createFromClass(parent, className);
    } else if (parent instanceof OCFunctionDefinition) {
        OCFunctionSymbol symbol = ((OCFunctionDefinition) parent).getSymbol();
        if (symbol != null) {
            OCSymbolWithQualifiedName<?> resolvedOwner = symbol.getResolvedOwner();
            if (resolvedOwner != null) {
                OCSymbol<?> owner = resolvedOwner.getDefinitionSymbol();
                if (owner instanceof OCStructSymbol && CidrGoogleTestUtil.isGoogleTestClass((OCStructSymbol) owner)) {
                    OCStruct struct = (OCStruct) owner.locateDefinition();
                    Couple<String> name = CidrGoogleTestUtil.extractGoogleTestName((OCStructSymbol) owner);
                    if (name != null) {
                        return createFromClassAndMethod(struct, name.first, name.second);
                    }
                    return createFromClass(struct, ((OCStructSymbol) owner).getQualifiedName().getName());
                }
            }
        }
    }
    // if we're still here, let's test for a macro and, as a last resort, a file.
    parent = PsiTreeUtil.getNonStrictParentOfType(element, OCMacroCall.class, OCFile.class);
    if (parent instanceof OCMacroCall) {
        OCMacroCall gtestMacro = CidrGoogleTestUtil.findGoogleTestMacros(parent);
        if (gtestMacro != null) {
            List<OCMacroCallArgument> arguments = gtestMacro.getArguments();
            if (arguments.size() >= 2) {
                OCMacroCallArgument suiteArg = arguments.get(0);
                OCMacroCallArgument testArg = arguments.get(1);
                // if the element is the first argument of macro call,
                // then running entire suite, otherwise only a current test
                boolean isSuite = isFirstArgument(PsiTreeUtil.getParentOfType(element, OCMacroCallArgument.class)) || isFirstArgument(element.getPrevSibling());
                String suiteName = CidrGoogleTestUtil.extractArgumentValue(suiteArg);
                String testName = CidrGoogleTestUtil.extractArgumentValue(testArg);
                OCStructSymbol symbol = CidrGoogleTestUtil.findGoogleTestSymbol(element.getProject(), suiteName, testName);
                if (symbol != null) {
                    OCStruct targetElement = (OCStruct) symbol.locateDefinition();
                    return createFromClassAndMethod(targetElement, suiteName, isSuite ? null : testName);
                }
            }
        }
        Couple<String> suite = CidrGoogleTestUtil.extractFullSuiteNameFromMacro(parent);
        if (suite != null) {
            Collection<OCStructSymbol> res = CidrGoogleTestUtil.findGoogleTestSymbolsForSuiteRandomly(element.getProject(), suite.first, true);
            if (res.size() != 0) {
                OCStruct struct = (OCStruct) res.iterator().next().locateDefinition();
                GoogleTestSpecification gtest = new GoogleTestSpecification.FromPsiElement(suite.first, null, suite.second, null);
                return new GoogleTestLocation(struct, gtest);
            }
        }
    } else if (parent instanceof OCFile) {
        return createFromFile(parent);
    }
    return null;
}
Also used : OCFunctionSymbol(com.jetbrains.cidr.lang.symbols.cpp.OCFunctionSymbol) OCFunctionDefinition(com.jetbrains.cidr.lang.psi.OCFunctionDefinition) Couple(com.intellij.openapi.util.Couple) OCSymbol(com.jetbrains.cidr.lang.symbols.OCSymbol) OCFile(com.jetbrains.cidr.lang.psi.OCFile) OCStruct(com.jetbrains.cidr.lang.psi.OCStruct) OCMacroCallArgument(com.jetbrains.cidr.lang.psi.OCMacroCallArgument) OCMacroCall(com.jetbrains.cidr.lang.psi.OCMacroCall) OCStructSymbol(com.jetbrains.cidr.lang.symbols.cpp.OCStructSymbol) OCSymbolWithQualifiedName(com.jetbrains.cidr.lang.symbols.cpp.OCSymbolWithQualifiedName) PsiElement(com.intellij.psi.PsiElement) Nullable(javax.annotation.Nullable)

Example 14 with OCFile

use of com.jetbrains.cidr.lang.psi.OCFile in project intellij by bazelbuild.

the class BlazeCppAutoImportHelperTest method resolve.

private void resolve(ProjectView projectView, TargetMap targetMap, OCFile... files) {
    BlazeProjectData blazeProjectData = projectDataBuilder().setTargetMap(targetMap).build();
    registerProjectService(BlazeProjectDataManager.class, new MockBlazeProjectDataManager(blazeProjectData));
    BlazeCWorkspace.getInstance(getProject()).update(new BlazeContext(), workspaceRoot, ProjectViewSet.builder().add(projectView).build(), blazeProjectData);
    for (OCFile file : files) {
        resetFileSymbols(file);
    }
    FileSymbolTablesCache.getInstance(getProject()).ensurePendingFilesProcessed();
}
Also used : OCFile(com.jetbrains.cidr.lang.psi.OCFile) BlazeContext(com.google.idea.blaze.base.scope.BlazeContext) BlazeProjectData(com.google.idea.blaze.base.model.BlazeProjectData) MockBlazeProjectDataManager(com.google.idea.blaze.base.model.MockBlazeProjectDataManager)

Example 15 with OCFile

use of com.jetbrains.cidr.lang.psi.OCFile in project intellij by bazelbuild.

the class BlazeCppAutoImportHelperTest method stlPathsUnderWorkspaceRoot_importStlHeader.

@Test
public void stlPathsUnderWorkspaceRoot_importStlHeader() {
    ProjectView projectView = projectView(directories("foo/bar"), targets("//foo/bar:bar"));
    TargetMap targetMap = TargetMapBuilder.builder().addTarget(createCcToolchain()).addTarget(createCcTarget("//foo/bar:bar", Kind.CC_LIBRARY, sources("foo/bar/bar.cc"), sources())).addTarget(createCcTarget("//third_party/stl:stl", Kind.CC_LIBRARY, sources(), sources("third_party/stl/vector.h"))).build();
    // Normally this is <vector> without .h, but we need to trick the file type detector into
    // realizing that this is an OCFile.
    OCFile header = createFile("third_party/stl/vector.h", "namespace std {", "template<typename T> class vector {};", "}");
    OCFile file = createFile("foo/bar/bar.cc", "std::vector<int> my_vector;");
    resolve(projectView, targetMap, file, header);
    testFixture.openFileInEditor(file.getVirtualFile());
    OCReferenceElement referenceElement = testFixture.findElementByText("std::vector<int>", OCReferenceElement.class);
    OCImportSymbolFix fix = new OCImportSymbolFix(referenceElement);
    assertThat(fix.isAvailable(getProject(), testFixture.getEditor(), file)).isTrue();
    assertThat(fix.getAutoImportItems()).hasSize(1);
    assertThat(fix.getAutoImportItems().get(0).getTitleAndLocation().getFirst()).isEqualTo("class 'std::vector'");
    assertThat(fix.getAutoImportItems().get(0).getTitleAndLocation().getSecond()).isEqualTo("<vector.h>");
}
Also used : OCFile(com.jetbrains.cidr.lang.psi.OCFile) OCReferenceElement(com.jetbrains.cidr.lang.psi.OCReferenceElement) ProjectView(com.google.idea.blaze.base.projectview.ProjectView) OCImportSymbolFix(com.jetbrains.cidr.lang.quickfixes.OCImportSymbolFix) TargetMap(com.google.idea.blaze.base.ideinfo.TargetMap) Test(org.junit.Test)

Aggregations

OCFile (com.jetbrains.cidr.lang.psi.OCFile)35 Test (org.junit.Test)28 PrivatePragma (com.google.idea.blaze.cpp.includes.IwyuPragmas.PrivatePragma)8 PsiFile (com.intellij.psi.PsiFile)5 TargetMap (com.google.idea.blaze.base.ideinfo.TargetMap)3 ProjectView (com.google.idea.blaze.base.projectview.ProjectView)3 Project (com.intellij.openapi.project.Project)3 OCReferenceElement (com.jetbrains.cidr.lang.psi.OCReferenceElement)3 OCImportSymbolFix (com.jetbrains.cidr.lang.quickfixes.OCImportSymbolFix)3 ImmutableList (com.google.common.collect.ImmutableList)2 GotoRelatedItem (com.intellij.navigation.GotoRelatedItem)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 List (java.util.List)2 Nullable (javax.annotation.Nullable)2 BlazeProjectData (com.google.idea.blaze.base.model.BlazeProjectData)1 MockBlazeProjectDataManager (com.google.idea.blaze.base.model.MockBlazeProjectDataManager)1 WorkspacePath (com.google.idea.blaze.base.model.primitives.WorkspacePath)1 BlazeContext (com.google.idea.blaze.base.scope.BlazeContext)1 PsiFileNode (com.intellij.ide.projectView.impl.nodes.PsiFileNode)1 Couple (com.intellij.openapi.util.Couple)1