Support template classes

Summary: public Translate template instantiations exported by clang.
There is no need to deal with templated code - clang creates one class
for each implementation.

Reviewed By: dulmarod

Differential Revision: D2686269

fb-gh-sync-id: 9249a00
master
Andrzej Kotulski 9 years ago committed by facebook-github-bot-7
parent 7389dfe976
commit ddbf4d9c47

@ -22,6 +22,7 @@ let decl_get_sub_decls decl =
let open Clang_ast_t in
match decl with
| CXXRecordDecl (_, _, _, _, decl_list, _, _, _)
| ClassTemplateSpecializationDecl (_, _, _, _, decl_list, _, _, _)
| RecordDecl (_, _, _, _, decl_list, _, _)
| ObjCInterfaceDecl (_, _, decl_list, _, _)
| ObjCProtocolDecl (_, _, decl_list, _, _)
@ -32,6 +33,8 @@ let decl_get_sub_decls decl =
| LinkageSpecDecl (_, decl_list, _)
| NamespaceDecl (_, _, decl_list, _, _) ->
decl_list
| ClassTemplateDecl (_, _, class_template_decl_info) ->
class_template_decl_info.ctdi_specializations
| _ ->
[]
@ -42,6 +45,8 @@ let decl_set_sub_decls decl decl_list' =
match decl with
| CXXRecordDecl (decl_info, name, opt_type, type_ptr, decl_list, decl_context_info, record_decl_info, cxx_record_info) ->
CXXRecordDecl (decl_info, name, opt_type, type_ptr, decl_list', decl_context_info, record_decl_info, cxx_record_info)
| ClassTemplateSpecializationDecl (decl_info, name, opt_type, type_ptr, decl_list, decl_context_info, record_decl_info, cxx_record_info) ->
ClassTemplateSpecializationDecl (decl_info, name, opt_type, type_ptr, decl_list', decl_context_info, record_decl_info, cxx_record_info)
| RecordDecl (decl_info, name, opt_type, type_ptr, decl_list, decl_context_info, record_decl_info) ->
RecordDecl (decl_info, name, opt_type, type_ptr, decl_list', decl_context_info, record_decl_info)
| ObjCInterfaceDecl (decl_info, name, decl_list, decl_context_info, obj_c_interface_decl_info) ->
@ -60,6 +65,9 @@ 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' = { ctdi_specializations = decl_list' } in
ClassTemplateDecl (decl_info, name, class_template_decl_info')
| _ ->
decl

@ -34,7 +34,9 @@ let rec translate_one_declaration tenv cg cfg parent_dec dec =
CMethod_declImpl.function_decl tenv cfg cg dec None
(* Currently C/C++ record decl treated in the same way *)
| CXXRecordDecl (_, _, _, _, decl_list, _, _, _) | RecordDecl (_, _, _, _, decl_list, _, _) ->
| ClassTemplateSpecializationDecl (_, _, _, _, decl_list, _, _, _)
| CXXRecordDecl (_, _, _, _, decl_list, _, _, _)
| RecordDecl (_, _, _, _, decl_list, _, _) ->
ignore (CTypes_decl.add_types_from_decl_to_tenv tenv dec);
let method_decls = CTypes_decl.get_method_decls dec decl_list in
let tranlate_method (parent, decl) =
@ -81,7 +83,8 @@ let rec translate_one_declaration tenv cg cfg parent_dec dec =
| Some ptr -> Ast_utils.get_decl ptr
| None -> Some parent_dec in
(match class_decl with
| Some (CXXRecordDecl _ as d) ->
| Some (CXXRecordDecl _ as d)
| Some (ClassTemplateSpecializationDecl _ as d) ->
let class_name = CTypes_decl.get_record_name d in
let curr_class = CContext.ContextCls(class_name, None, []) in
if !CFrontend_config.testing_mode then
@ -96,6 +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.ctdi_specializations in
IList.iter (translate_one_declaration tenv cg cfg dec) decl_list
| dec -> ()
(* Translates a file by translating the ast into a cfg. *)

@ -86,7 +86,8 @@ let get_record_name_csu decl =
let open Clang_ast_t in
let name_info, opt_type, should_be_class = match decl with
| RecordDecl (_, name_info, opt_type, _, _, _, _) -> name_info, opt_type, false
| CXXRecordDecl (_, name_info, opt_type, _, _, _, _, cxx_record_info) ->
| CXXRecordDecl (_, name_info, opt_type, _, _, _, _, cxx_record_info)
| ClassTemplateSpecializationDecl (_, name_info, opt_type, _, _, _, _, cxx_record_info) ->
(* we use Sil.Class for C++ because we expect Sil.Class csu from *)
(* types that have methods. And in C++ struct/class/union can have methods *)
name_info, opt_type, not cxx_record_info.xrdi_is_c_like
@ -102,6 +103,7 @@ let get_method_decls parent decl_list =
let open Clang_ast_t in
let rec traverse_decl parent decl = match decl with
| CXXMethodDecl _ | CXXConstructorDecl _ -> [(parent, decl)]
| ClassTemplateSpecializationDecl (_, _, _, _, decl_list', _, _, _)
| CXXRecordDecl (_, _, _, _, decl_list', _, _, _)
| RecordDecl (_, _, _, _, decl_list', _, _) -> traverse_decl_list decl decl_list'
| _ -> []
@ -121,8 +123,10 @@ let get_class_methods tenv class_name decl_list =
IList.flatten_options (IList.map process_method_decl decl_list)
let get_superclass_decls decl =
let open Clang_ast_t in
match decl with
| Clang_ast_t.CXXRecordDecl (_, _, _, _, _, _, _, cxx_rec_info) ->
| CXXRecordDecl (_, _, _, _, _, _, _, cxx_rec_info)
| ClassTemplateSpecializationDecl (_, _, _, _, _, _, _, cxx_rec_info) ->
(* there is no concept of virtual inheritance in the backend right now *)
let base_ptr = cxx_rec_info.Clang_ast_t.xrdi_bases @ cxx_rec_info.Clang_ast_t.xrdi_vbases in
IList.map Ast_utils.get_decl_from_typ_ptr base_ptr
@ -146,6 +150,7 @@ let add_struct_to_tenv tenv typ =
let rec get_struct_fields tenv decl =
let open Clang_ast_t in
let decl_list = match decl with
| ClassTemplateSpecializationDecl (_, _, _, _, decl_list, _, _, _)
| CXXRecordDecl (_, _, _, _, decl_list, _, _, _)
| RecordDecl (_, _, _, _, decl_list, _, _) -> decl_list
| _ -> [] in
@ -155,6 +160,7 @@ let rec get_struct_fields tenv decl =
let typ = type_ptr_to_sil_type tenv type_ptr in
let annotation_items = [] in (* For the moment we don't use them*)
[(id, typ, annotation_items)]
| ClassTemplateSpecializationDecl (decl_info, _, _, _, _, _, _, _)
| CXXRecordDecl (decl_info, _, _, _, _, _, _, _)
| RecordDecl (decl_info, _, _, _, _, _, _) ->
(* C++/C Records treated in the same way*)
@ -166,11 +172,12 @@ let rec get_struct_fields tenv decl =
IList.flatten (base_class_fields @ (IList.map do_one_decl decl_list))
(* For a record declaration it returns/constructs the type *)
and get_strct_cpp_class_declaration_type tenv decl =
and get_struct_cpp_class_declaration_type tenv decl =
let open Clang_ast_t in
match decl with
| CXXRecordDecl (decl_info, name_info, opt_type, type_ptr, decl_list, _, record_decl_info, _)
| RecordDecl (decl_info, name_info, opt_type, type_ptr, decl_list, _, record_decl_info) ->
| ClassTemplateSpecializationDecl (_, _, _, type_ptr, decl_list, _, record_decl_info, _)
| CXXRecordDecl (_, _, _, type_ptr, decl_list, _, record_decl_info, _)
| RecordDecl (_, _, _, type_ptr, decl_list, _, record_decl_info) ->
let csu, name = get_record_name_csu decl in
let mangled_name = Mangled.from_string name in
let sil_typename = Sil.Tvar (Sil.TN_csu (csu, mangled_name)) in
@ -198,9 +205,8 @@ and get_strct_cpp_class_declaration_type tenv decl =
and add_types_from_decl_to_tenv tenv decl =
let open Clang_ast_t in
match decl with
| CXXRecordDecl (decl_info, name_info, opt_type, type_ptr, decl_list, _, record_decl_info, _)
| RecordDecl (decl_info, name_info, opt_type, type_ptr, decl_list, _, record_decl_info) ->
get_strct_cpp_class_declaration_type tenv decl
| ClassTemplateSpecializationDecl _ | CXXRecordDecl _ | RecordDecl _ ->
get_struct_cpp_class_declaration_type tenv decl
| ObjCInterfaceDecl _ -> ObjcInterface_decl.interface_declaration type_ptr_to_sil_type tenv decl
| ObjCImplementationDecl _ ->
ObjcInterface_decl.interface_impl_declaration type_ptr_to_sil_type tenv decl

@ -0,0 +1,50 @@
/*
* 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 Choose1 {
int div(int a, int b) { return 1 / a; }
};
struct Choose2 {
int div(int a, int b) { return 1 / b; }
// extra method that is not present in Choose1
int extra(int a) { return 1 / a; }
};
template<class T>
struct ExecStore {
T f;
int call_div(int a) {
// this will always cause div by 0 for Choose2
return f.div(a, 0);
}
};
int choose1_div0(ExecStore<Choose1> &s) {
return s.call_div(0);
}
int choose1_div1(ExecStore<Choose1> &s) {
return s.call_div(1);
}
int choose2_div0_no_report(ExecStore<Choose2> &s) {
// error is already reported for ExecStore<Choose2>::call_div()
return s.call_div(1);
}
int choose2_div0_extra(ExecStore<Choose2> &s) {
return s.f.extra(0);
}
int choose2_div1_extra(ExecStore<Choose2> &s) {
return s.f.extra(1);
}

@ -0,0 +1,112 @@
digraph iCFG {
30 [label="30: Return Stmt \n n$0=*&s:class ExecStore<Choose2> & [line 49]\n n$1=_fun_Choose2_extra(n$0.f:class Choose2 &,1:int ) [line 49]\n *&return:int =n$1 [line 49]\n REMOVE_TEMPS(n$0,n$1); [line 49]\n NULLIFY(&s,false); [line 49]\n APPLY_ABSTRACTION; [line 49]\n " shape="box"]
30 -> 29 ;
29 [label="29: Exit choose2_div1_extra \n " color=yellow style=filled]
28 [label="28: Start choose2_div1_extra\nFormals: s:class ExecStore<Choose2> &\nLocals: \n DECLARE_LOCALS(&return); [line 48]\n " color=yellow style=filled]
28 -> 30 ;
27 [label="27: Return Stmt \n n$0=*&s:class ExecStore<Choose2> & [line 45]\n n$1=_fun_Choose2_extra(n$0.f:class Choose2 &,0:int ) [line 45]\n *&return:int =n$1 [line 45]\n REMOVE_TEMPS(n$0,n$1); [line 45]\n NULLIFY(&s,false); [line 45]\n APPLY_ABSTRACTION; [line 45]\n " shape="box"]
27 -> 26 ;
26 [label="26: Exit choose2_div0_extra \n " color=yellow style=filled]
25 [label="25: Start choose2_div0_extra\nFormals: s:class ExecStore<Choose2> &\nLocals: \n DECLARE_LOCALS(&return); [line 44]\n " color=yellow style=filled]
25 -> 27 ;
24 [label="24: Return Stmt \n n$0=*&s:class ExecStore<Choose2> & [line 41]\n n$1=_fun_ExecStore<Choose2>_call_div(n$0:class ExecStore<Choose2> &,1:int ) [line 41]\n *&return:int =n$1 [line 41]\n REMOVE_TEMPS(n$0,n$1); [line 41]\n NULLIFY(&s,false); [line 41]\n APPLY_ABSTRACTION; [line 41]\n " shape="box"]
24 -> 23 ;
23 [label="23: Exit choose2_div0_no_report \n " color=yellow style=filled]
22 [label="22: Start choose2_div0_no_report\nFormals: s:class ExecStore<Choose2> &\nLocals: \n DECLARE_LOCALS(&return); [line 39]\n " color=yellow style=filled]
22 -> 24 ;
21 [label="21: Return Stmt \n n$0=*&s:class ExecStore<Choose1> & [line 36]\n n$1=_fun_ExecStore<Choose1>_call_div(n$0:class ExecStore<Choose1> &,1:int ) [line 36]\n *&return:int =n$1 [line 36]\n REMOVE_TEMPS(n$0,n$1); [line 36]\n NULLIFY(&s,false); [line 36]\n APPLY_ABSTRACTION; [line 36]\n " shape="box"]
21 -> 20 ;
20 [label="20: Exit choose1_div1 \n " color=yellow style=filled]
19 [label="19: Start choose1_div1\nFormals: s:class ExecStore<Choose1> &\nLocals: \n DECLARE_LOCALS(&return); [line 35]\n " color=yellow style=filled]
19 -> 21 ;
18 [label="18: Return Stmt \n n$0=*&s:class ExecStore<Choose1> & [line 32]\n n$1=_fun_ExecStore<Choose1>_call_div(n$0:class ExecStore<Choose1> &,0:int ) [line 32]\n *&return:int =n$1 [line 32]\n REMOVE_TEMPS(n$0,n$1); [line 32]\n NULLIFY(&s,false); [line 32]\n APPLY_ABSTRACTION; [line 32]\n " shape="box"]
18 -> 17 ;
17 [label="17: Exit choose1_div0 \n " color=yellow style=filled]
16 [label="16: Start choose1_div0\nFormals: s:class ExecStore<Choose1> &\nLocals: \n DECLARE_LOCALS(&return); [line 31]\n " color=yellow style=filled]
16 -> 18 ;
15 [label="15: Return Stmt \n n$0=*&this:class ExecStore<Choose2> * [line 26]\n n$1=*&a:int [line 26]\n n$2=_fun_Choose2_div(n$0.f:class Choose2 &,n$1:int ,0:int ) [line 26]\n *&return:int =n$2 [line 26]\n REMOVE_TEMPS(n$0,n$1,n$2); [line 26]\n NULLIFY(&a,false); [line 26]\n NULLIFY(&this,false); [line 26]\n APPLY_ABSTRACTION; [line 26]\n " shape="box"]
15 -> 14 ;
14 [label="14: Exit ExecStore<Choose2>_call_div \n " color=yellow style=filled]
13 [label="13: Start ExecStore<Choose2>_call_div\nFormals: this:class ExecStore<Choose2> * a:int \nLocals: \n DECLARE_LOCALS(&return); [line 24]\n " color=yellow style=filled]
13 -> 15 ;
12 [label="12: Return Stmt \n n$0=*&this:class ExecStore<Choose1> * [line 26]\n n$1=*&a:int [line 26]\n n$2=_fun_Choose1_div(n$0.f:class Choose1 &,n$1:int ,0:int ) [line 26]\n *&return:int =n$2 [line 26]\n REMOVE_TEMPS(n$0,n$1,n$2); [line 26]\n NULLIFY(&a,false); [line 26]\n NULLIFY(&this,false); [line 26]\n APPLY_ABSTRACTION; [line 26]\n " shape="box"]
12 -> 11 ;
11 [label="11: Exit ExecStore<Choose1>_call_div \n " color=yellow style=filled]
10 [label="10: Start ExecStore<Choose1>_call_div\nFormals: this:class ExecStore<Choose1> * a:int \nLocals: \n DECLARE_LOCALS(&return); [line 24]\n " color=yellow style=filled]
10 -> 12 ;
9 [label="9: Return Stmt \n n$0=*&a:int [line 18]\n *&return:int =(1 / n$0) [line 18]\n REMOVE_TEMPS(n$0); [line 18]\n NULLIFY(&a,false); [line 18]\n APPLY_ABSTRACTION; [line 18]\n " shape="box"]
9 -> 8 ;
8 [label="8: Exit Choose2_extra \n " color=yellow style=filled]
7 [label="7: Start Choose2_extra\nFormals: this:class Choose2 * a:int \nLocals: \n DECLARE_LOCALS(&return); [line 18]\n NULLIFY(&this,false); [line 18]\n " color=yellow style=filled]
7 -> 9 ;
6 [label="6: Return Stmt \n n$0=*&b:int [line 15]\n *&return:int =(1 / n$0) [line 15]\n REMOVE_TEMPS(n$0); [line 15]\n NULLIFY(&b,false); [line 15]\n APPLY_ABSTRACTION; [line 15]\n " shape="box"]
6 -> 5 ;
5 [label="5: Exit Choose2_div \n " color=yellow style=filled]
4 [label="4: Start Choose2_div\nFormals: this:class Choose2 * a:int b:int \nLocals: \n DECLARE_LOCALS(&return); [line 15]\n NULLIFY(&a,false); [line 15]\n NULLIFY(&this,false); [line 15]\n " color=yellow style=filled]
4 -> 6 ;
3 [label="3: Return Stmt \n n$0=*&a:int [line 11]\n *&return:int =(1 / n$0) [line 11]\n REMOVE_TEMPS(n$0); [line 11]\n NULLIFY(&a,false); [line 11]\n APPLY_ABSTRACTION; [line 11]\n " shape="box"]
3 -> 2 ;
2 [label="2: Exit Choose1_div \n " color=yellow style=filled]
1 [label="1: Start Choose1_div\nFormals: this:class Choose1 * a:int b:int \nLocals: \n DECLARE_LOCALS(&return); [line 11]\n NULLIFY(&b,false); [line 11]\n NULLIFY(&this,false); [line 11]\n " color=yellow style=filled]
1 -> 3 ;
}

@ -0,0 +1,27 @@
/*
* 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.
*/
template<class T>
struct Container {
T field;
};
struct X {
int field;
};
int div0_template_field(Container<int> &v) {
v.field = 0;
return 1 / v.field;
}
int div0_struct_field(X &v) {
v.field = 0;
return 1 / v.field;
}

@ -0,0 +1,32 @@
digraph iCFG {
8 [label="8: BinaryOperatorStmt: Assign \n n$2=*&v:struct X & [line 25]\n *n$2.field:int =0 [line 25]\n REMOVE_TEMPS(n$2); [line 25]\n " shape="box"]
8 -> 7 ;
7 [label="7: Return Stmt \n n$0=*&v:struct X & [line 26]\n n$1=*n$0.field:int [line 26]\n *&return:int =(1 / n$1) [line 26]\n REMOVE_TEMPS(n$0,n$1); [line 26]\n NULLIFY(&v,false); [line 26]\n APPLY_ABSTRACTION; [line 26]\n " shape="box"]
7 -> 6 ;
6 [label="6: Exit div0_struct_field \n " color=yellow style=filled]
5 [label="5: Start div0_struct_field\nFormals: v:struct X &\nLocals: \n DECLARE_LOCALS(&return); [line 24]\n " color=yellow style=filled]
5 -> 8 ;
4 [label="4: BinaryOperatorStmt: Assign \n n$2=*&v:struct Container<int> & [line 20]\n *n$2.field:int =0 [line 20]\n REMOVE_TEMPS(n$2); [line 20]\n " shape="box"]
4 -> 3 ;
3 [label="3: Return Stmt \n n$0=*&v:struct Container<int> & [line 21]\n n$1=*n$0.field:int [line 21]\n *&return:int =(1 / n$1) [line 21]\n REMOVE_TEMPS(n$0,n$1); [line 21]\n NULLIFY(&v,false); [line 21]\n APPLY_ABSTRACTION; [line 21]\n " shape="box"]
3 -> 2 ;
2 [label="2: Exit div0_template_field \n " color=yellow style=filled]
1 [label="1: Start div0_template_field\nFormals: v:struct Container<int> &\nLocals: \n DECLARE_LOCALS(&return); [line 19]\n " color=yellow style=filled]
1 -> 4 ;
}

@ -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 ClassTemplateTest {
public static final String FILE =
"infer/tests/codetoanalyze/cpp/frontend/templates/class_template_instantiate.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 = {
"choose1_div0",
"choose2_div0_extra",
"ExecStore<Choose2>_call_div",
};
InferResults inferResults = InferRunner.runInferCPP(inferCmd);
assertThat(
"Results should contain divide by 0 error",
inferResults,
containsExactly(
DIVIDE_BY_ZERO,
FILE,
procedures
)
);
}
}

@ -0,0 +1,44 @@
/*
* Copyright (c) 2013 - 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 frontend.cpp;
import org.junit.Rule;
import org.junit.Test;
import java.io.IOException;
import utils.DebuggableTemporaryFolder;
import utils.InferException;
import utils.ClangFrontendUtils;
public class TemplatesTest {
String basePath = "infer/tests/codetoanalyze/cpp/frontend/templates/";
@Rule
public DebuggableTemporaryFolder folder = new DebuggableTemporaryFolder();
void frontendTest(String fileRelative) throws InterruptedException, IOException, InferException {
ClangFrontendUtils.createAndCompareCppDotFiles(folder, basePath + fileRelative);
}
@Test
public void testSimpleDotFilesMatch()
throws InterruptedException, IOException, InferException {
frontendTest("simple.cpp");
}
@Test
public void testClassTemplateInstantiateDotFilesMatch()
throws InterruptedException, IOException, InferException {
frontendTest("class_template_instantiate.cpp");
}
}
Loading…
Cancel
Save