2
0
Эх сурвалжийг харах

added warnings and dce for unused pure calls (close #778)

Nicolas Cannasse 5 жил өмнө
parent
commit
2c2d6918dc
3 өөрчлөгдсөн 17 нэмэгдсэн , 2 устгасан
  1. 8 1
      hxsl/Ast.hx
  2. 6 1
      hxsl/Checker.hx
  3. 3 0
      hxsl/Macros.hx

+ 8 - 1
hxsl/Ast.hx

@@ -426,7 +426,14 @@ class Tools {
 			return hasSideEffect(e) || hasSideEffect(index);
 		case TConst(_), TVar(_), TGlobal(_):
 			return false;
-		case TVarDecl(_), TCall(_), TDiscard, TContinue, TBreak, TReturn(_):
+		case TCall(e, pl):
+			if( !e.e.match(TGlobal(_)) )
+				return true;
+			for( p in pl )
+				if( hasSideEffect(p) )
+					return true;
+			return false;
+		case TVarDecl(_), TDiscard, TContinue, TBreak, TReturn(_):
 			return true;
 		case TSwitch(e, cases, def):
 			for( c in cases ) {

+ 6 - 1
hxsl/Checker.hx

@@ -196,6 +196,9 @@ class Checker {
 		return Ast.Error.t(msg,pos);
 	}
 
+	public dynamic function warning( msg : String, pos : Position ) {
+	}
+
 	public dynamic function loadShader( path : String ) : Expr {
 		throw "Not implemented";
 		return null;
@@ -380,7 +383,9 @@ class Checker {
 				case EVars(_): InBlock;
 				default: if( el.length == 0 ) with else NoValue;
 				}
-				tl.push(typeExpr(e, ew));
+				var et = typeExpr(e, ew);
+				if( el.length != 0 && !et.hasSideEffect() ) warning("This expression has no side effect", e.pos);
+				tl.push(et);
 			}
 			vars = old;
 			type = with == NoValue ? TVoid : tl[tl.length - 1].t;

+ 3 - 0
hxsl/Macros.hx

@@ -387,6 +387,9 @@ class Macros {
 						var name = Std.string(c);
 						var check = new Checker();
 						check.loadShader = loadShader;
+						check.warning = function(msg,pos) {
+							haxe.macro.Context.warning(msg, pos);
+						};
 						var shader = check.check(name, shader);
 						//Printer.check(shader);
 						var str = Context.defined("display") ? "" : Serializer.run(shader);