From cb42fdcc3126695f6d5433a32fc2a4a4c272345b Mon Sep 17 00:00:00 2001 From: Andrzej Kotulski Date: Thu, 19 Nov 2015 03:03:36 -0800 Subject: [PATCH] Use fully qualified names for functions Summary: public Functions had unqualified names before. Fix it. Reviewed By: dulmarod Differential Revision: D2668647 fb-gh-sync-id: cfeddea --- infer/src/clang/cMethod_trans.ml | 2 +- infer/src/clang/cTrans.ml | 2 +- .../cpp/frontend/namespace/function.cpp | 26 ++++++++ .../cpp/frontend/namespace/function.cpp.dot | 57 +++++++++++++++++ .../cpp/frontend/namespace/namespace.cpp.dot | 12 ++-- .../endtoend/cpp/NamespaceFunctionTest.java | 64 +++++++++++++++++++ infer/tests/frontend/cpp/NamespaceTest.java | 17 +++-- 7 files changed, 168 insertions(+), 12 deletions(-) create mode 100644 infer/tests/codetoanalyze/cpp/frontend/namespace/function.cpp create mode 100644 infer/tests/codetoanalyze/cpp/frontend/namespace/function.cpp.dot create mode 100644 infer/tests/endtoend/cpp/NamespaceFunctionTest.java diff --git a/infer/src/clang/cMethod_trans.ml b/infer/src/clang/cMethod_trans.ml index 795c2aeaa..f03fa2215 100644 --- a/infer/src/clang/cMethod_trans.ml +++ b/infer/src/clang/cMethod_trans.ml @@ -114,7 +114,7 @@ let method_signature_of_decl meth_decl block_data_opt = let open Clang_ast_t in match meth_decl, block_data_opt with | FunctionDecl (decl_info, name_info, tp, fdi), _ -> - let name = name_info.ni_name in + let name = Ast_utils.get_qualified_name name_info in let language = !CFrontend_config.language in let func_decl = Func_decl_info (fdi, tp, language) in let function_info = Some (decl_info, fdi) in diff --git a/infer/src/clang/cTrans.ml b/infer/src/clang/cTrans.ml index 00d7bece7..9457279cb 100644 --- a/infer/src/clang/cTrans.ml +++ b/infer/src/clang/cTrans.ml @@ -303,7 +303,7 @@ struct let open CContext in let context = trans_state.context in let name_info, decl_ptr, type_ptr = get_info_from_decl_ref decl_ref in - let name = name_info.Clang_ast_t.ni_name in + let name = Ast_utils.get_qualified_name name_info in let typ = CTypes_decl.type_ptr_to_sil_type context.tenv type_ptr in let pname = if CTrans_models.is_modeled_builtin name then diff --git a/infer/tests/codetoanalyze/cpp/frontend/namespace/function.cpp b/infer/tests/codetoanalyze/cpp/frontend/namespace/function.cpp new file mode 100644 index 000000000..5fcec808c --- /dev/null +++ b/infer/tests/codetoanalyze/cpp/frontend/namespace/function.cpp @@ -0,0 +1,26 @@ +/* +* Copyright (c) 2015 - present Facebook, Inc. +* All rights reserved. +* +* This source code is licensed under the BSD style license found in the +* LICENSE file in the root directory of this source tree. An additional grant +* of patent rights can be found in the PATENTS file in the same directory. +*/ + +namespace f1 { + int get() { return 1;} + int get0() { return 0;} +} + +namespace f2 { + int get() { return -1;} +} + +int div0_using() { + using namespace f1; + return 1 / get0(); +} + +int div0_namespace_resolution() { + return 1 / (f1::get() + f2::get()); +} diff --git a/infer/tests/codetoanalyze/cpp/frontend/namespace/function.cpp.dot b/infer/tests/codetoanalyze/cpp/frontend/namespace/function.cpp.dot new file mode 100644 index 000000000..cce454012 --- /dev/null +++ b/infer/tests/codetoanalyze/cpp/frontend/namespace/function.cpp.dot @@ -0,0 +1,57 @@ +digraph iCFG { +15 [label="15: Return Stmt \n n$0=_fun_f1::get() [line 25]\n n$1=_fun_f2::get() [line 25]\n *&return:int =(1 / (n$0 + n$1)) [line 25]\n REMOVE_TEMPS(n$0,n$1); [line 25]\n APPLY_ABSTRACTION; [line 25]\n " shape="box"] + + + 15 -> 14 ; +14 [label="14: Exit div0_namespace_resolution \n " color=yellow style=filled] + + +13 [label="13: Start div0_namespace_resolution\nFormals: \nLocals: \n DECLARE_LOCALS(&return); [line 24]\n " color=yellow style=filled] + + + 13 -> 15 ; +12 [label="12: Return Stmt \n n$0=_fun_f1::get0() [line 21]\n *&return:int =(1 / n$0) [line 21]\n REMOVE_TEMPS(n$0); [line 21]\n APPLY_ABSTRACTION; [line 21]\n " shape="box"] + + + 12 -> 11 ; +11 [label="11: Exit div0_using \n " color=yellow style=filled] + + +10 [label="10: Start div0_using\nFormals: \nLocals: \n DECLARE_LOCALS(&return); [line 19]\n " color=yellow style=filled] + + + 10 -> 12 ; +9 [label="9: Return Stmt \n *&return:int =-1 [line 16]\n APPLY_ABSTRACTION; [line 16]\n " shape="box"] + + + 9 -> 8 ; +8 [label="8: Exit f2::get \n " color=yellow style=filled] + + +7 [label="7: Start f2::get\nFormals: \nLocals: \n DECLARE_LOCALS(&return); [line 16]\n " color=yellow style=filled] + + + 7 -> 9 ; +6 [label="6: Return Stmt \n *&return:int =0 [line 12]\n APPLY_ABSTRACTION; [line 12]\n " shape="box"] + + + 6 -> 5 ; +5 [label="5: Exit f1::get0 \n " color=yellow style=filled] + + +4 [label="4: Start f1::get0\nFormals: \nLocals: \n DECLARE_LOCALS(&return); [line 12]\n " color=yellow style=filled] + + + 4 -> 6 ; +3 [label="3: Return Stmt \n *&return:int =1 [line 11]\n APPLY_ABSTRACTION; [line 11]\n " shape="box"] + + + 3 -> 2 ; +2 [label="2: Exit f1::get \n " color=yellow style=filled] + + +1 [label="1: Start f1::get\nFormals: \nLocals: \n DECLARE_LOCALS(&return); [line 11]\n " color=yellow style=filled] + + + 1 -> 3 ; +} diff --git a/infer/tests/codetoanalyze/cpp/frontend/namespace/namespace.cpp.dot b/infer/tests/codetoanalyze/cpp/frontend/namespace/namespace.cpp.dot index cc8ef09e9..176751d4f 100644 --- a/infer/tests/codetoanalyze/cpp/frontend/namespace/namespace.cpp.dot +++ b/infer/tests/codetoanalyze/cpp/frontend/namespace/namespace.cpp.dot @@ -23,11 +23,11 @@ digraph iCFG { 19 -> 18 ; -18 [label="18: BinaryOperatorStmt: Assign \n n$2=_fun_value() [line 55]\n *&i:int =n$2 [line 55]\n REMOVE_TEMPS(n$2); [line 55]\n NULLIFY(&i,false); [line 55]\n " shape="box"] +18 [label="18: BinaryOperatorStmt: Assign \n n$2=_fun_foo::value() [line 55]\n *&i:int =n$2 [line 55]\n REMOVE_TEMPS(n$2); [line 55]\n NULLIFY(&i,false); [line 55]\n " shape="box"] 18 -> 17 ; -17 [label="17: BinaryOperatorStmt: Assign \n n$1=_fun_value() [line 56]\n *&i:int =n$1 [line 56]\n REMOVE_TEMPS(n$1); [line 56]\n NULLIFY(&i,false); [line 56]\n " shape="box"] +17 [label="17: BinaryOperatorStmt: Assign \n n$1=_fun_bar::value() [line 56]\n *&i:int =n$1 [line 56]\n REMOVE_TEMPS(n$1); [line 56]\n NULLIFY(&i,false); [line 56]\n " shape="box"] 17 -> 16 ; @@ -57,10 +57,10 @@ digraph iCFG { 10 -> 9 ; -9 [label="9: Exit value \n " color=yellow style=filled] +9 [label="9: Exit bar::value \n " color=yellow style=filled] -8 [label="8: Start value\nFormals: \nLocals: \n DECLARE_LOCALS(&return); [line 29]\n " color=yellow style=filled] +8 [label="8: Start bar::value\nFormals: \nLocals: \n DECLARE_LOCALS(&return); [line 29]\n " color=yellow style=filled] 8 -> 10 ; @@ -75,10 +75,10 @@ digraph iCFG { 5 -> 4 ; -4 [label="4: Exit value \n " color=yellow style=filled] +4 [label="4: Exit foo::value \n " color=yellow style=filled] -3 [label="3: Start value\nFormals: \nLocals: \n DECLARE_LOCALS(&return); [line 16]\n " color=yellow style=filled] +3 [label="3: Start foo::value\nFormals: \nLocals: \n DECLARE_LOCALS(&return); [line 16]\n " color=yellow style=filled] 3 -> 5 ; diff --git a/infer/tests/endtoend/cpp/NamespaceFunctionTest.java b/infer/tests/endtoend/cpp/NamespaceFunctionTest.java new file mode 100644 index 000000000..73ab265f3 --- /dev/null +++ b/infer/tests/endtoend/cpp/NamespaceFunctionTest.java @@ -0,0 +1,64 @@ +/* +* Copyright (c) 2015 - present Facebook, Inc. +* All rights reserved. +* +* This source code is licensed under the BSD style license found in the +* LICENSE file in the root directory of this source tree. An additional grant +* of patent rights can be found in the PATENTS file in the same directory. +*/ + +package endtoend.cpp; + +import static org.hamcrest.MatcherAssert.assertThat; +import static utils.matchers.ResultContainsExactly.containsExactly; + +import com.google.common.collect.ImmutableList; + +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; + +import java.io.IOException; + +import utils.DebuggableTemporaryFolder; +import utils.InferException; +import utils.InferResults; +import utils.InferRunner; + +public class NamespaceFunctionTest { + + public static final String FILE = + "infer/tests/codetoanalyze/cpp/frontend/namespace/function.cpp"; + + private static ImmutableList inferCmd; + + public static final String DIVIDE_BY_ZERO = "DIVIDE_BY_ZERO"; + + @ClassRule + public static DebuggableTemporaryFolder folder = + new DebuggableTemporaryFolder(); + + @BeforeClass + public static void runInfer() throws InterruptedException, IOException { + inferCmd = InferRunner.createCPPInferCommand(folder, FILE); + } + + @Test + public void whenInferRunsOnDiv0MethodsErrorIsFound() + throws InterruptedException, IOException, InferException { + InferResults inferResults = InferRunner.runInferCPP(inferCmd); + String[] procedures = { + "div0_using", + "div0_namespace_resolution", + }; + assertThat( + "Results should contain the expected divide by zero", + inferResults, + containsExactly( + DIVIDE_BY_ZERO, + FILE, + procedures + ) + ); + } +} diff --git a/infer/tests/frontend/cpp/NamespaceTest.java b/infer/tests/frontend/cpp/NamespaceTest.java index 9090f946f..751b167a6 100644 --- a/infer/tests/frontend/cpp/NamespaceTest.java +++ b/infer/tests/frontend/cpp/NamespaceTest.java @@ -19,15 +19,24 @@ import utils.InferException; import utils.ClangFrontendUtils; public class NamespaceTest { + String basePath = "infer/tests/codetoanalyze/cpp/frontend/namespace/"; @Rule public DebuggableTemporaryFolder folder = new DebuggableTemporaryFolder(); + void frontendTest(String fileRelative) throws InterruptedException, IOException, InferException { + ClangFrontendUtils.createAndCompareCppDotFiles(folder, basePath + fileRelative); + } + + @Test + public void testNamespaceDotFilesMatch() + throws InterruptedException, IOException, InferException { + frontendTest("namespace.cpp"); + } + @Test - public void whenCaptureRunCommaThenDotFilesAreTheSame() + public void testFunctionDotFilesMatch() throws InterruptedException, IOException, InferException { - String src = - "infer/tests/codetoanalyze/cpp/frontend/namespace/namespace.cpp"; - ClangFrontendUtils.createAndCompareCppDotFiles(folder, src); + frontendTest("function.cpp"); } }