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);
}
}
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;
}
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;
}
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));
}
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);
}
}
Aggregations