From 76203aa84790e74760d846e5d9eaf37b1ce6cb28 Mon Sep 17 00:00:00 2001 From: Andrzej Kotulski Date: Fri, 26 Jun 2015 13:02:27 -0100 Subject: [PATCH] [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(nullptr); // <- reports now //X *x = (X*)nullptr; // <- reported before return x->a; } --- infer/src/clang/cTrans.ml | 3 +- .../cpp/frontend/types/casts.cpp | 4 ++ .../cpp/frontend/types/casts.dot | 13 +++++ infer/tests/frontend/cpp/CastsTest.java | 47 +++++++++++++++++++ 4 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 infer/tests/codetoanalyze/cpp/frontend/types/casts.cpp create mode 100644 infer/tests/codetoanalyze/cpp/frontend/types/casts.dot create mode 100644 infer/tests/frontend/cpp/CastsTest.java diff --git a/infer/src/clang/cTrans.ml b/infer/src/clang/cTrans.ml index a87260265..130e5df73 100644 --- a/infer/src/clang/cTrans.ml +++ b/infer/src/clang/cTrans.ml @@ -1701,7 +1701,8 @@ struct | ObjCBridgedCastExpr(stmt_info, stmt_list, expr_info, cast_kind, _) -> cast_exprs_trans trans_state stmt_info stmt_list expr_info cast_kind true | 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 | IntegerLiteral(stmt_info, _, expr_info, integer_literal_info) -> diff --git a/infer/tests/codetoanalyze/cpp/frontend/types/casts.cpp b/infer/tests/codetoanalyze/cpp/frontend/types/casts.cpp new file mode 100644 index 000000000..d018fcac7 --- /dev/null +++ b/infer/tests/codetoanalyze/cpp/frontend/types/casts.cpp @@ -0,0 +1,4 @@ +void stat_cast() { + int a; + long long la = static_cast(a); +} diff --git a/infer/tests/codetoanalyze/cpp/frontend/types/casts.dot b/infer/tests/codetoanalyze/cpp/frontend/types/casts.dot new file mode 100644 index 000000000..f9777d120 --- /dev/null +++ b/infer/tests/codetoanalyze/cpp/frontend/types/casts.dot @@ -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 ; +} diff --git a/infer/tests/frontend/cpp/CastsTest.java b/infer/tests/frontend/cpp/CastsTest.java new file mode 100644 index 000000000..7de431601 --- /dev/null +++ b/infer/tests/frontend/cpp/CastsTest.java @@ -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 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)); + } +}