[Frontend][C++] Add translation for static cast

Summary:
@public
Translate CXXStaticCastExpr

Test Plan:
Add test, confirm that it gets translated.
Also create example to see that infer reports null dereference with this change:
  struct X { int a; };
  int main() {
    X *x = static_cast<X*>(nullptr); // <- reports now
    //X *x = (X*)nullptr; // <- reported before
    return x->a;
  }
master
Andrzej Kotulski 10 years ago
parent 7f72397999
commit 76203aa847

@ -1701,7 +1701,8 @@ struct
| ObjCBridgedCastExpr(stmt_info, stmt_list, expr_info, cast_kind, _) -> | ObjCBridgedCastExpr(stmt_info, stmt_list, expr_info, cast_kind, _) ->
cast_exprs_trans trans_state stmt_info stmt_list expr_info cast_kind true cast_exprs_trans trans_state stmt_info stmt_list expr_info cast_kind true
| ImplicitCastExpr(stmt_info, stmt_list, expr_info, cast_kind) | ImplicitCastExpr(stmt_info, stmt_list, expr_info, cast_kind)
| CStyleCastExpr(stmt_info, stmt_list, expr_info, cast_kind, _) -> | CStyleCastExpr(stmt_info, stmt_list, expr_info, cast_kind, _)
| CXXStaticCastExpr(stmt_info, stmt_list, expr_info, cast_kind, _, _) ->
cast_exprs_trans trans_state stmt_info stmt_list expr_info cast_kind false cast_exprs_trans trans_state stmt_info stmt_list expr_info cast_kind false
| IntegerLiteral(stmt_info, _, expr_info, integer_literal_info) -> | IntegerLiteral(stmt_info, _, expr_info, integer_literal_info) ->

@ -0,0 +1,4 @@
void stat_cast() {
int a;
long long la = static_cast<long long>(a);
}

@ -0,0 +1,13 @@
digraph iCFG {
3 [label="3: DeclStmt \n n$0=*&a:int [line 3]\n *&la:long long =n$0 [line 3]\n REMOVE_TEMPS(n$0); [line 3]\n NULLIFY(&a,false); [line 3]\n NULLIFY(&la,false); [line 3]\n APPLY_ABSTRACTION; [line 3]\n " shape="box"]
3 -> 2 ;
2 [label="2: Exit stat_cast \n " color=yellow style=filled]
1 [label="1: Start stat_cast\nFormals: \nLocals: a:int la:long long \n DECLARE_LOCALS(&return,&a,&la); [line 1]\n NULLIFY(&la,false); [line 1]\n " color=yellow style=filled]
1 -> 3 ;
}

@ -0,0 +1,47 @@
/*
* Copyright (c) 2013- Facebook.
* All rights reserved.
*/
package frontend.cpp;
import static org.hamcrest.MatcherAssert.assertThat;
import static utils.matchers.DotFilesEqual.dotFileEqualTo;
import com.google.common.collect.ImmutableList;
import org.junit.Rule;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import utils.DebuggableTemporaryFolder;
import utils.InferException;
import utils.InferRunner;
public class CastsTest {
@Rule
public DebuggableTemporaryFolder folder = new DebuggableTemporaryFolder();
@Test
public void whenCaptureRunCommaThenDotFilesAreTheSame()
throws InterruptedException, IOException, InferException {
String literal_src =
"infer/tests/codetoanalyze/cpp/frontend/types/casts.cpp";
String literal_dotty =
"infer/tests/codetoanalyze/cpp/frontend/types/casts.dot";
ImmutableList<String> inferCmd =
InferRunner.createCPPInferCommandFrontend(
folder,
literal_src);
File newDotFile = InferRunner.runInferFrontend(inferCmd);
assertThat(
"In the capture of " + literal_src +
" the dotty files should be the same.",
newDotFile, dotFileEqualTo(literal_dotty));
}
}
Loading…
Cancel
Save