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: 9249a00master
							parent
							
								
									7389dfe976
								
							
						
					
					
						commit
						ddbf4d9c47
					
				| @ -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…
					
					
				
		Reference in new issue