Search in sources :

Example 11 with DiagnosticPos

use of org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos in project ballerina by ballerina-lang.

the class TaintAnalyzer method visitAssignment.

private void visitAssignment(BLangExpression varRefExpr, boolean varTaintedStatus, DiagnosticPos pos) {
    // Generate error if a global variable has been assigned with a tainted value.
    if (varTaintedStatus && varRefExpr instanceof BLangVariableReference) {
        BLangVariableReference varRef = (BLangVariableReference) varRefExpr;
        if (varRef.symbol != null && varRef.symbol.owner != null) {
            if (varRef.symbol.owner instanceof BPackageSymbol || SymbolKind.SERVICE.equals(varRef.symbol.owner.kind) || SymbolKind.CONNECTOR.equals(varRef.symbol.owner.kind)) {
                addTaintError(pos, varRef.symbol.name.value, DiagnosticCode.TAINTED_VALUE_PASSED_TO_GLOBAL_VARIABLE);
                return;
            }
        }
    }
    // TODO: Re-evaluating the full data-set (array) when a change occur.
    if (varRefExpr instanceof BLangIndexBasedAccess) {
        nonOverridingAnalysis = true;
        updatedVarRefTaintedState((BLangIndexBasedAccess) varRefExpr, varTaintedStatus);
        nonOverridingAnalysis = false;
    } else if (varRefExpr instanceof BLangFieldBasedAccess) {
        BLangFieldBasedAccess fieldBasedAccessExpr = (BLangFieldBasedAccess) varRefExpr;
        // Propagate tainted status to fields, when field symbols are present (Example: structs).
        if (fieldBasedAccessExpr.symbol != null) {
            setTaintedStatus(fieldBasedAccessExpr, varTaintedStatus);
        }
        nonOverridingAnalysis = true;
        updatedVarRefTaintedState(fieldBasedAccessExpr, varTaintedStatus);
        nonOverridingAnalysis = false;
    } else {
        BLangVariableReference varRef = (BLangVariableReference) varRefExpr;
        setTaintedStatus(varRef, varTaintedStatus);
    }
}
Also used : BLangIndexBasedAccess(org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess) BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) BLangFieldBasedAccess(org.wso2.ballerinalang.compiler.tree.expressions.BLangFieldBasedAccess) BLangVariableReference(org.wso2.ballerinalang.compiler.tree.expressions.BLangVariableReference)

Example 12 with DiagnosticPos

use of org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos in project ballerina by ballerina-lang.

the class EndpointSPIAnalyzer method isValidEndpointSPI.

private boolean isValidEndpointSPI(DiagnosticPos pos, BStructSymbol structSymbol) {
    if (isProcessedValidEndpoint(structSymbol)) {
        return true;
    }
    if (isProcessedInvalidEndpoint(structSymbol)) {
        // Check for cached values, to avoid duplication of endpoint related error messages.
        dlog.error(pos, DiagnosticCode.ENDPOINT_INVALID_TYPE, structSymbol);
        return false;
    }
    Endpoint ep = new Endpoint(pos, structSymbol);
    processEndpointSPIFunctions(ep);
    // Check validity of endpoint interface.
    checkValidBaseEndpointSPI(ep);
    checkValidInteractableEndpoint(ep);
    checkValidStartableEndpoint(ep);
    checkValidStoppableEndpoint(ep);
    checkValidRegistrableEndpoint(ep);
    if (invalidSPIs.containsKey(ep.structSymbol)) {
        return false;
    }
    validSPIs.put(structSymbol, ep);
    return true;
}
Also used : BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Example 13 with DiagnosticPos

use of org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos in project ballerina by ballerina-lang.

the class EndpointSPIAnalyzer method getEndpointTypeFromServiceType.

public BStructType getEndpointTypeFromServiceType(DiagnosticPos pos, BType type) {
    if (type.tag != TypeTags.STRUCT) {
        dlog.error(pos, DiagnosticCode.ENDPOINT_STRUCT_TYPE_REQUIRED);
        return null;
    }
    final BStructSymbol serviceType = (BStructSymbol) type.tsymbol;
    for (BStructSymbol.BAttachedFunction attachedFunc : serviceType.attachedFuncs) {
        if (Names.EP_SERVICE_GET_ENDPOINT.equals(attachedFunc.funcName)) {
            if (attachedFunc.type.getParameterTypes().size() != 0 || attachedFunc.type.retTypes.size() != 1 || attachedFunc.type.retTypes.get(0).tag != TypeTags.STRUCT) {
                dlog.error(pos, DiagnosticCode.SERVICE_INVALID_STRUCT_TYPE, serviceType);
                return null;
            }
            final BStructSymbol endPointType = (BStructSymbol) attachedFunc.type.retTypes.get(0).tsymbol;
            if (isValidEndpointSPI(pos, endPointType)) {
                return (BStructType) attachedFunc.type.retTypes.get(0);
            }
            break;
        }
    }
    dlog.error(pos, DiagnosticCode.SERVICE_INVALID_STRUCT_TYPE, serviceType);
    return null;
}
Also used : BStructType(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)

Example 14 with DiagnosticPos

use of org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos in project ballerina by ballerina-lang.

the class SemanticAnalyzer method handleSafeAssignmentWithVarDeclaration.

private BType handleSafeAssignmentWithVarDeclaration(DiagnosticPos pos, BType rhsType) {
    if (rhsType.tag != TypeTags.UNION && types.isAssignable(symTable.errStructType, rhsType)) {
        dlog.error(pos, DiagnosticCode.SAFE_ASSIGN_STMT_INVALID_USAGE);
        return symTable.errType;
    } else if (rhsType.tag != TypeTags.UNION) {
        return rhsType;
    }
    // Collect all the rhs types from the union type
    boolean isErrorFound = false;
    BUnionType unionType = (BUnionType) rhsType;
    Set<BType> rhsTypeSet = new HashSet<>(unionType.memberTypes);
    for (BType type : unionType.memberTypes) {
        if (types.isAssignable(type, symTable.errStructType)) {
            rhsTypeSet.remove(type);
            isErrorFound = true;
        }
    }
    if (rhsTypeSet.isEmpty() || !isErrorFound) {
        dlog.error(pos, DiagnosticCode.SAFE_ASSIGN_STMT_INVALID_USAGE);
        return symTable.noType;
    } else if (rhsTypeSet.size() == 1) {
        return rhsTypeSet.toArray(new BType[0])[0];
    }
    return new BUnionType(null, rhsTypeSet, rhsTypeSet.contains(symTable.nullType));
}
Also used : BUnionType(org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) HashSet(java.util.HashSet)

Example 15 with DiagnosticPos

use of org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos in project ballerina by ballerina-lang.

the class Parser method generateCompilationUnit.

private CompilationUnitNode generateCompilationUnit(PackageSourceEntry sourceEntry) {
    try {
        int prevErrCount = dlog.errorCount;
        BDiagnosticSource diagnosticSrc = getDiagnosticSource(sourceEntry);
        String entryName = sourceEntry.getEntryName();
        BLangCompilationUnit compUnit = (BLangCompilationUnit) TreeBuilder.createCompilationUnit();
        compUnit.setName(sourceEntry.getEntryName());
        compUnit.pos = new DiagnosticPos(diagnosticSrc, 1, 1, 1, 1);
        ANTLRInputStream ais = new ANTLRInputStream(new ByteArrayInputStream(sourceEntry.getCode()));
        ais.name = entryName;
        BallerinaLexer lexer = new BallerinaLexer(ais);
        lexer.removeErrorListeners();
        lexer.addErrorListener(new BallerinaParserErrorListener(context, diagnosticSrc));
        CommonTokenStream tokenStream = new CommonTokenStream(lexer);
        BallerinaParser parser = new BallerinaParser(tokenStream);
        parser.setErrorHandler(getErrorStrategy(diagnosticSrc));
        parser.addParseListener(newListener(tokenStream, compUnit, diagnosticSrc));
        parser.compilationUnit();
        return compUnit;
    } catch (IOException e) {
        throw new RuntimeException("Error in populating package model: " + e.getMessage(), e);
    }
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) BDiagnosticSource(org.wso2.ballerinalang.compiler.util.diagnotic.BDiagnosticSource) IOException(java.io.IOException) BallerinaParser(org.wso2.ballerinalang.compiler.parser.antlr4.BallerinaParser) DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) ByteArrayInputStream(java.io.ByteArrayInputStream) BallerinaParserErrorListener(org.wso2.ballerinalang.compiler.parser.antlr4.BallerinaParserErrorListener) BLangCompilationUnit(org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit) BallerinaLexer(org.wso2.ballerinalang.compiler.parser.antlr4.BallerinaLexer) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Aggregations

DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)56 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)33 BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)22 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)17 ArrayList (java.util.ArrayList)15 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)14 Whitespace (org.ballerinalang.model.Whitespace)13 BLangVariableDef (org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)12 BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)11 BLangInvocation (org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation)11 IdentifierNode (org.ballerinalang.model.tree.IdentifierNode)9 SelectExpressionNode (org.ballerinalang.model.tree.clauses.SelectExpressionNode)9 ExpressionNode (org.ballerinalang.model.tree.expressions.ExpressionNode)9 BLangNameReference (org.wso2.ballerinalang.compiler.tree.BLangNameReference)9 BLangBinaryExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr)9 BLangExpressionStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt)9 BLangIdentifier (org.wso2.ballerinalang.compiler.tree.BLangIdentifier)8 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)8 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)8 BLangXMLQuotedString (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString)8