Translate C++ function templates

Summary:
public
Read definitions of C++ function template specializations. Infer still doesn't work correctly for template methods, it will be addressed later

Reviewed By: dulmarod

Differential Revision: D2707411

fb-gh-sync-id: 6072796
master
Andrzej Kotulski 9 years ago committed by facebook-github-bot-5
parent 4dc9dac9e9
commit 0bf7e46579

@ -33,8 +33,9 @@ let decl_get_sub_decls decl =
| LinkageSpecDecl (_, decl_list, _)
| NamespaceDecl (_, _, decl_list, _, _) ->
decl_list
| ClassTemplateDecl (_, _, class_template_decl_info) ->
class_template_decl_info.tdi_specializations
| ClassTemplateDecl (_, _, template_decl_info)
| FunctionTemplateDecl (_, _, template_decl_info) ->
template_decl_info.tdi_specializations
| _ ->
[]
@ -65,9 +66,12 @@ let decl_set_sub_decls decl decl_list' =
LinkageSpecDecl (decl_info, decl_list', decl_context_info)
| NamespaceDecl (decl_info, name, decl_list, decl_context_info, namespace_decl_info) ->
NamespaceDecl (decl_info, name, decl_list', decl_context_info, namespace_decl_info)
| ClassTemplateDecl (decl_info, name, class_template_decl_info) ->
let class_template_decl_info' = { tdi_specializations = decl_list' } in
ClassTemplateDecl (decl_info, name, class_template_decl_info')
| ClassTemplateDecl (decl_info, name, template_decl_info) ->
let template_decl_info' = { tdi_specializations = decl_list' } in
ClassTemplateDecl (decl_info, name, template_decl_info')
| FunctionTemplateDecl (decl_info, name, template_decl_info) ->
let template_decl_info' = { tdi_specializations = decl_list' } in
FunctionTemplateDecl (decl_info, name, template_decl_info')
| _ ->
decl

@ -99,8 +99,9 @@ let rec translate_one_declaration tenv cg cfg parent_dec dec =
IList.iter (translate_one_declaration tenv cg cfg dec) decl_list
| NamespaceDecl (decl_info, name_info, decl_list, decl_context_info, _) ->
IList.iter (translate_one_declaration tenv cg cfg dec) decl_list
| ClassTemplateDecl (decl_info, named_decl_info, class_template_decl_info) ->
let decl_list = class_template_decl_info.Clang_ast_t.tdi_specializations in
| ClassTemplateDecl (decl_info, named_decl_info, template_decl_info)
| FunctionTemplateDecl (decl_info, named_decl_info, template_decl_info) ->
let decl_list = template_decl_info.Clang_ast_t.tdi_specializations in
IList.iter (translate_one_declaration tenv cg cfg dec) decl_list
| dec -> ()

@ -0,0 +1,73 @@
/*
* 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.
*/
struct X1 {
int getVal() { return 1; }
};
struct X2 {
int getVal() { return 0; }
};
struct X3 {
int get() { return 0; }
};
template<class T>
int getVal(T &x) {
return x.getVal();
}
// explicit specialization
template<>
int getVal(X3 &x) {
return x.get();
}
template<class T>
int createAndGetVal() {
T x;
return getVal(x);
}
template<class T>
int createAndDiv() {
return 1 / createAndGetVal<T>();
}
// explicit instantiaion - will report div by 0
template int createAndDiv<X3>();
// explicit instantiation - won't report div by 0
template int createAndDiv<X1>();
// no instantiation for X2 - won't report div by 0
// if line below is not commented, infer would report
// division by 0
// template int createAndDiv<X2>();
int div0_get_val() {
X1 x1;
X3 x3;
return getVal(x1) / getVal(x3);
}
int div1_get_val() {
X1 x1;
X3 x3;
return getVal(x3) / getVal(x1);
}
int div0_create_and_get_val() {
return createAndGetVal<X1>() / createAndGetVal<X3>();
}
int div1_create_and_get_val() {
return createAndGetVal<X3>() / createAndGetVal<X1>();
}

@ -0,0 +1,183 @@
digraph iCFG {
49 [label="49: Return Stmt \n n$0=_fun_createAndGetVal<struct X3>() [line 70]\n n$1=_fun_createAndGetVal<struct X1>() [line 70]\n *&return:int =(n$0 / n$1) [line 70]\n REMOVE_TEMPS(n$0,n$1); [line 70]\n APPLY_ABSTRACTION; [line 70]\n " shape="box"]
49 -> 48 ;
48 [label="48: Exit div1_create_and_get_val \n " color=yellow style=filled]
47 [label="47: Start div1_create_and_get_val\nFormals: \nLocals: \n DECLARE_LOCALS(&return); [line 69]\n " color=yellow style=filled]
47 -> 49 ;
46 [label="46: Return Stmt \n n$0=_fun_createAndGetVal<struct X1>() [line 66]\n n$1=_fun_createAndGetVal<struct X3>() [line 66]\n *&return:int =(n$0 / n$1) [line 66]\n REMOVE_TEMPS(n$0,n$1); [line 66]\n APPLY_ABSTRACTION; [line 66]\n " shape="box"]
46 -> 45 ;
45 [label="45: Exit div0_create_and_get_val \n " color=yellow style=filled]
44 [label="44: Start div0_create_and_get_val\nFormals: \nLocals: \n DECLARE_LOCALS(&return); [line 65]\n " color=yellow style=filled]
44 -> 46 ;
43 [label="43: Call _fun_X1_X1 \n _fun_X1_X1(&x1:class X1 *) [line 60]\n " shape="box"]
43 -> 42 ;
42 [label="42: Call _fun_X3_X3 \n _fun_X3_X3(&x3:class X3 *) [line 61]\n " shape="box"]
42 -> 41 ;
41 [label="41: Return Stmt \n n$0=_fun_getVal<struct X3>(&x3:class X3 &) [line 62]\n n$1=_fun_getVal<struct X1>(&x1:class X1 &) [line 62]\n *&return:int =(n$0 / n$1) [line 62]\n REMOVE_TEMPS(n$0,n$1); [line 62]\n NULLIFY(&x1,false); [line 62]\n NULLIFY(&x3,false); [line 62]\n APPLY_ABSTRACTION; [line 62]\n " shape="box"]
41 -> 40 ;
40 [label="40: Exit div1_get_val \n " color=yellow style=filled]
39 [label="39: Start div1_get_val\nFormals: \nLocals: x3:class X3 x1:class X1 \n DECLARE_LOCALS(&return,&x3,&x1); [line 59]\n " color=yellow style=filled]
39 -> 43 ;
38 [label="38: Call _fun_X1_X1 \n _fun_X1_X1(&x1:class X1 *) [line 54]\n " shape="box"]
38 -> 37 ;
37 [label="37: Call _fun_X3_X3 \n _fun_X3_X3(&x3:class X3 *) [line 55]\n " shape="box"]
37 -> 36 ;
36 [label="36: Return Stmt \n n$0=_fun_getVal<struct X1>(&x1:class X1 &) [line 56]\n n$1=_fun_getVal<struct X3>(&x3:class X3 &) [line 56]\n *&return:int =(n$0 / n$1) [line 56]\n REMOVE_TEMPS(n$0,n$1); [line 56]\n NULLIFY(&x1,false); [line 56]\n NULLIFY(&x3,false); [line 56]\n APPLY_ABSTRACTION; [line 56]\n " shape="box"]
36 -> 35 ;
35 [label="35: Exit div0_get_val \n " color=yellow style=filled]
34 [label="34: Start div0_get_val\nFormals: \nLocals: x3:class X3 x1:class X1 \n DECLARE_LOCALS(&return,&x3,&x1); [line 53]\n " color=yellow style=filled]
34 -> 38 ;
33 [label="33: Return Stmt \n n$0=_fun_createAndGetVal<struct X1>() [line 41]\n *&return:int =(1 / n$0) [line 41]\n REMOVE_TEMPS(n$0); [line 41]\n APPLY_ABSTRACTION; [line 41]\n " shape="box"]
33 -> 32 ;
32 [label="32: Exit createAndDiv<struct X1> \n " color=yellow style=filled]
31 [label="31: Start createAndDiv<struct X1>\nFormals: \nLocals: \n DECLARE_LOCALS(&return); [line 40]\n " color=yellow style=filled]
31 -> 33 ;
30 [label="30: Return Stmt \n n$0=_fun_createAndGetVal<struct X3>() [line 41]\n *&return:int =(1 / n$0) [line 41]\n REMOVE_TEMPS(n$0); [line 41]\n APPLY_ABSTRACTION; [line 41]\n " shape="box"]
30 -> 29 ;
29 [label="29: Exit createAndDiv<struct X3> \n " color=yellow style=filled]
28 [label="28: Start createAndDiv<struct X3>\nFormals: \nLocals: \n DECLARE_LOCALS(&return); [line 40]\n " color=yellow style=filled]
28 -> 30 ;
27 [label="27: Call _fun_X1_X1 \n _fun_X1_X1(&x:class X1 *) [line 35]\n " shape="box"]
27 -> 26 ;
26 [label="26: Return Stmt \n n$0=_fun_getVal<struct X1>(&x:class X1 &) [line 36]\n *&return:int =n$0 [line 36]\n REMOVE_TEMPS(n$0); [line 36]\n NULLIFY(&x,false); [line 36]\n APPLY_ABSTRACTION; [line 36]\n " shape="box"]
26 -> 25 ;
25 [label="25: Exit createAndGetVal<struct X1> \n " color=yellow style=filled]
24 [label="24: Start createAndGetVal<struct X1>\nFormals: \nLocals: x:class X1 \n DECLARE_LOCALS(&return,&x); [line 34]\n " color=yellow style=filled]
24 -> 27 ;
23 [label="23: Call _fun_X3_X3 \n _fun_X3_X3(&x:class X3 *) [line 35]\n " shape="box"]
23 -> 22 ;
22 [label="22: Return Stmt \n n$0=_fun_getVal<struct X3>(&x:class X3 &) [line 36]\n *&return:int =n$0 [line 36]\n REMOVE_TEMPS(n$0); [line 36]\n NULLIFY(&x,false); [line 36]\n APPLY_ABSTRACTION; [line 36]\n " shape="box"]
22 -> 21 ;
21 [label="21: Exit createAndGetVal<struct X3> \n " color=yellow style=filled]
20 [label="20: Start createAndGetVal<struct X3>\nFormals: \nLocals: x:class X3 \n DECLARE_LOCALS(&return,&x); [line 34]\n " color=yellow style=filled]
20 -> 23 ;
19 [label="19: Return Stmt \n n$0=*&x:class X3 & [line 30]\n n$1=_fun_X3_get(n$0:class X3 &) [line 30]\n *&return:int =n$1 [line 30]\n REMOVE_TEMPS(n$0,n$1); [line 30]\n NULLIFY(&x,false); [line 30]\n APPLY_ABSTRACTION; [line 30]\n " shape="box"]
19 -> 18 ;
18 [label="18: Exit getVal<struct X3> \n " color=yellow style=filled]
17 [label="17: Start getVal<struct X3>\nFormals: x:class X3 &\nLocals: \n DECLARE_LOCALS(&return); [line 28]\n " color=yellow style=filled]
17 -> 19 ;
16 [label="16: Return Stmt \n n$0=*&x:class X1 & [line 24]\n n$1=_fun_X1_getVal(n$0:class X1 &) [line 24]\n *&return:int =n$1 [line 24]\n REMOVE_TEMPS(n$0,n$1); [line 24]\n NULLIFY(&x,false); [line 24]\n APPLY_ABSTRACTION; [line 24]\n " shape="box"]
16 -> 15 ;
15 [label="15: Exit getVal<struct X1> \n " color=yellow style=filled]
14 [label="14: Start getVal<struct X1>\nFormals: x:class X1 &\nLocals: \n DECLARE_LOCALS(&return); [line 23]\n " color=yellow style=filled]
14 -> 16 ;
13 [label="13: Exit X3_X3 \n " color=yellow style=filled]
12 [label="12: Start X3_X3\nFormals: this:class X3 *\nLocals: \n DECLARE_LOCALS(&return); [line 18]\n NULLIFY(&this,false); [line 18]\n " color=yellow style=filled]
12 -> 13 ;
11 [label="11: Return Stmt \n *&return:int =0 [line 19]\n APPLY_ABSTRACTION; [line 19]\n " shape="box"]
11 -> 10 ;
10 [label="10: Exit X3_get \n " color=yellow style=filled]
9 [label="9: Start X3_get\nFormals: this:class X3 *\nLocals: \n DECLARE_LOCALS(&return); [line 19]\n NULLIFY(&this,false); [line 19]\n " color=yellow style=filled]
9 -> 11 ;
8 [label="8: Return Stmt \n *&return:int =0 [line 15]\n APPLY_ABSTRACTION; [line 15]\n " shape="box"]
8 -> 7 ;
7 [label="7: Exit X2_getVal \n " color=yellow style=filled]
6 [label="6: Start X2_getVal\nFormals: this:class X2 *\nLocals: \n DECLARE_LOCALS(&return); [line 15]\n NULLIFY(&this,false); [line 15]\n " color=yellow style=filled]
6 -> 8 ;
5 [label="5: Exit X1_X1 \n " color=yellow style=filled]
4 [label="4: Start X1_X1\nFormals: this:class X1 *\nLocals: \n DECLARE_LOCALS(&return); [line 10]\n NULLIFY(&this,false); [line 10]\n " color=yellow style=filled]
4 -> 5 ;
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 X1_getVal \n " color=yellow style=filled]
1 [label="1: Start X1_getVal\nFormals: this:class X1 *\nLocals: \n DECLARE_LOCALS(&return); [line 11]\n NULLIFY(&this,false); [line 11]\n " color=yellow style=filled]
1 -> 3 ;
}

@ -0,0 +1,66 @@
/*
* 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 FunctionTemplateTest {
public static final String FILE =
"infer/tests/codetoanalyze/cpp/frontend/templates/function.cpp";
private static ImmutableList<String> 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 whenInferRunsOnDiv0FunctionsErrorIsFound()
throws InterruptedException, IOException, InferException {
String[] procedures = {
"div0_get_val",
"div0_create_and_get_val",
"createAndDiv<struct X3>",
};
InferResults inferResults = InferRunner.runInferCPP(inferCmd);
assertThat(
"Results should contain divide by 0 error",
inferResults,
containsExactly(
DIVIDE_BY_ZERO,
FILE,
procedures
)
);
}
}

@ -41,4 +41,9 @@ public class TemplatesTest {
frontendTest("class_template_instantiate.cpp");
}
@Test
public void testFunctionDotFilesMatch()
throws InterruptedException, IOException, InferException {
frontendTest("function.cpp");
}
}

Loading…
Cancel
Save