Summary: With this approach, all the global consts will be inlined in the places where they are used. Reviewed By: dulmarod Differential Revision: D3703133 fbshipit-source-id: 3c19479master
parent
6840efdd1c
commit
40b176fd01
@ -0,0 +1,22 @@
|
||||
/*
|
||||
* Copyright (c) 2016 - 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 X {
|
||||
X() {}
|
||||
};
|
||||
const X global;
|
||||
|
||||
X test() { return global; }
|
||||
|
||||
static const int v = 2;
|
||||
|
||||
int test2() {
|
||||
int local = v;
|
||||
return v;
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
/* @generated */
|
||||
digraph iCFG {
|
||||
11 [label="11: DeclStmt \n *&#GB$v:int =2 [line 20]\n n$1=*&#GB$v:int [line 20]\n *&local:int =n$1 [line 20]\n " shape="box"]
|
||||
|
||||
|
||||
11 -> 10 ;
|
||||
10 [label="10: Return Stmt \n *&#GB$v:int =2 [line 21]\n n$0=*&#GB$v:int [line 21]\n *&return:int =n$0 [line 21]\n " shape="box"]
|
||||
|
||||
|
||||
10 -> 9 ;
|
||||
9 [label="9: Exit test2 \n " color=yellow style=filled]
|
||||
|
||||
|
||||
8 [label="8: Start test2\nFormals: \nLocals: local:int \n DECLARE_LOCALS(&return,&local); [line 19]\n " color=yellow style=filled]
|
||||
|
||||
|
||||
8 -> 11 ;
|
||||
7 [label="7: Return Stmt \n n$0=*&__return_param:class X * [line 15]\n _fun_X_X(&#GB$global:class X *) [line 13]\n _fun_X_X(n$0:class X *,&#GB$global:class X &) [line 15]\n " shape="box"]
|
||||
|
||||
|
||||
7 -> 6 ;
|
||||
6 [label="6: Exit test \n " color=yellow style=filled]
|
||||
|
||||
|
||||
5 [label="5: Start test\nFormals: __return_param:class X *\nLocals: \n DECLARE_LOCALS(&return); [line 15]\n " color=yellow style=filled]
|
||||
|
||||
|
||||
5 -> 7 ;
|
||||
4 [label="4: Exit X_X \n " color=yellow style=filled]
|
||||
|
||||
|
||||
3 [label="3: Start X_X\nFormals: this:class X * __param_0:class X &\nLocals: \n DECLARE_LOCALS(&return); [line 10]\n " color=yellow style=filled]
|
||||
|
||||
|
||||
3 -> 4 ;
|
||||
2 [label="2: Exit X_X \n " color=yellow style=filled]
|
||||
|
||||
|
||||
1 [label="1: Start X_X\nFormals: this:class X *\nLocals: \n DECLARE_LOCALS(&return); [line 11]\n " color=yellow style=filled]
|
||||
|
||||
|
||||
1 -> 2 ;
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
/*
|
||||
* Copyright (c) 2016 - 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.
|
||||
*/
|
||||
|
||||
const int global = 1 ? 2 : 3;
|
||||
int test() { return global; }
|
@ -0,0 +1,35 @@
|
||||
/* @generated */
|
||||
digraph iCFG {
|
||||
8 [label="8: Return Stmt \n n$1=*&0$?%__sil_tmpSIL_temp_conditional___n$0:int [line 10]\n *&#GB$global:int =n$1 [line 11]\n n$2=*&#GB$global:int [line 11]\n *&return:int =n$2 [line 11]\n " shape="box"]
|
||||
|
||||
|
||||
8 -> 2 ;
|
||||
7 [label="7: ConditinalStmt Branch \n *&0$?%__sil_tmpSIL_temp_conditional___n$0:int =3 [line 10]\n " shape="box"]
|
||||
|
||||
|
||||
7 -> 3 ;
|
||||
6 [label="6: ConditinalStmt Branch \n *&0$?%__sil_tmpSIL_temp_conditional___n$0:int =2 [line 10]\n " shape="box"]
|
||||
|
||||
|
||||
6 -> 3 ;
|
||||
5 [label="5: Prune (false branch) \n PRUNE((1 == 0), false); [line 10]\n " shape="invhouse"]
|
||||
|
||||
|
||||
5 -> 7 ;
|
||||
4 [label="4: Prune (true branch) \n PRUNE((1 != 0), true); [line 10]\n " shape="invhouse"]
|
||||
|
||||
|
||||
4 -> 6 ;
|
||||
3 [label="3: + \n " ]
|
||||
|
||||
|
||||
3 -> 8 ;
|
||||
2 [label="2: Exit test \n " color=yellow style=filled]
|
||||
|
||||
|
||||
1 [label="1: Start test\nFormals: \nLocals: 0$?%__sil_tmpSIL_temp_conditional___n$0:int \n DECLARE_LOCALS(&return,&0$?%__sil_tmpSIL_temp_conditional___n$0); [line 11]\n " color=yellow style=filled]
|
||||
|
||||
|
||||
1 -> 4 ;
|
||||
1 -> 5 ;
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright (c) 2016 - 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.
|
||||
*/
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
static NSString* const GlobalConst = @"A Global Const!";
|
||||
static NSString* Global = @"A Global!";
|
||||
|
||||
@interface SimpleRoot : NSObject
|
||||
|
||||
@end
|
||||
|
||||
@implementation SimpleRoot
|
||||
|
||||
- (void)doSomethingOkWithDict:(NSMutableDictionary*)dict
|
||||
andString:(NSString*)input {
|
||||
NSString* str = [GlobalConst stringByAppendingString:input];
|
||||
[dict removeObjectForKey:str];
|
||||
}
|
||||
|
||||
- (void)doSomethingBadWithDict:(NSMutableDictionary*)dict
|
||||
andString:(NSString*)input {
|
||||
NSString* str = [Global stringByAppendingString:input];
|
||||
[dict removeObjectForKey:str];
|
||||
}
|
||||
|
||||
@end
|
@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Copyright (c) 2016 - 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.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
float field1;
|
||||
float field2;
|
||||
float field3;
|
||||
} Fields;
|
||||
|
||||
static const Fields __someFields = {
|
||||
.field1 = 1, .field2 = 2, .field3 = 3,
|
||||
};
|
||||
|
||||
Fields fields() { return __someFields; }
|
@ -0,0 +1,33 @@
|
||||
/* @generated */
|
||||
digraph iCFG {
|
||||
8 [label="8: Return Stmt \n n$0=*&__return_param:class Fields * [line 20]\n *&#GB$__someFields.field1:float =1 [line 16]\n *&#GB$__someFields.field2:float =2 [line 16]\n *&#GB$__someFields.field3:float =3 [line 16]\n _fun_Fields_(n$0:class Fields *,&#GB$__someFields:class Fields &) [line 20]\n " shape="box"]
|
||||
|
||||
|
||||
8 -> 7 ;
|
||||
7 [label="7: Exit fields \n " color=yellow style=filled]
|
||||
|
||||
|
||||
6 [label="6: Start fields\nFormals: __return_param:class Fields *\nLocals: \n DECLARE_LOCALS(&return); [line 20]\n " color=yellow style=filled]
|
||||
|
||||
|
||||
6 -> 8 ;
|
||||
5 [label="5: Constructor Init \n n$6=*&this:class Fields * [line 10]\n n$7=*&__param_0:class Fields & [line 10]\n n$8=*n$7.field1:float [line 10]\n *n$6.field1:float =n$8 [line 10]\n " shape="box"]
|
||||
|
||||
|
||||
5 -> 4 ;
|
||||
4 [label="4: Constructor Init \n n$3=*&this:class Fields * [line 10]\n n$4=*&__param_0:class Fields & [line 10]\n n$5=*n$4.field2:float [line 10]\n *n$3.field2:float =n$5 [line 10]\n " shape="box"]
|
||||
|
||||
|
||||
4 -> 3 ;
|
||||
3 [label="3: Constructor Init \n n$0=*&this:class Fields * [line 10]\n n$1=*&__param_0:class Fields & [line 10]\n n$2=*n$1.field3:float [line 10]\n *n$0.field3:float =n$2 [line 10]\n " shape="box"]
|
||||
|
||||
|
||||
3 -> 2 ;
|
||||
2 [label="2: Exit Fields_ \n " color=yellow style=filled]
|
||||
|
||||
|
||||
1 [label="1: Start Fields_\nFormals: this:class Fields * __param_0:class Fields &\nLocals: \n DECLARE_LOCALS(&return); [line 10]\n " color=yellow style=filled]
|
||||
|
||||
|
||||
1 -> 5 ;
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright (c) 2016 - 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.objc.infer;
|
||||
|
||||
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 GlobalConstNPETest {
|
||||
|
||||
public static final String FILE =
|
||||
"infer/tests/codetoanalyze/objc/errors/global_const/global_const.m";
|
||||
|
||||
private static ImmutableList<String> inferCmd;
|
||||
|
||||
public static final String NULL_DEREFERENCE = "NULL_DEREFERENCE";
|
||||
|
||||
@ClassRule
|
||||
public static DebuggableTemporaryFolder folder =
|
||||
new DebuggableTemporaryFolder();
|
||||
|
||||
@BeforeClass
|
||||
public static void runInfer() throws InterruptedException, IOException {
|
||||
inferCmd = InferRunner.createObjCInferCommand(folder, FILE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInferRunsOnMain1ThenDivideByZeroIsFound()
|
||||
throws InterruptedException, IOException, InferException {
|
||||
InferResults inferResults = InferRunner.runInferObjC(inferCmd);
|
||||
String[] procedures = {"doSomethingBadWithDict:andString:"};
|
||||
assertThat(
|
||||
"Results should contain the expected " + NULL_DEREFERENCE,
|
||||
inferResults,
|
||||
containsExactly(NULL_DEREFERENCE, FILE, procedures)
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright (c) 2016 - 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.ClangFrontendUtils;
|
||||
import utils.DebuggableTemporaryFolder;
|
||||
import utils.InferException;
|
||||
|
||||
public class GlobalConstTest {
|
||||
|
||||
@Rule
|
||||
public DebuggableTemporaryFolder folder = new DebuggableTemporaryFolder();
|
||||
|
||||
@Test
|
||||
public void whenCaptureRunCommaThenDotFilesAreTheSame()
|
||||
throws InterruptedException, IOException, InferException {
|
||||
String src =
|
||||
"infer/tests/codetoanalyze/cpp/frontend/global_const/global_const1.cpp";
|
||||
ClangFrontendUtils.createAndCompareCppDotFiles(folder, src);
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright (c) 2016 - 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.ClangFrontendUtils;
|
||||
import utils.DebuggableTemporaryFolder;
|
||||
import utils.InferException;
|
||||
|
||||
public class GlobalConstTest2 {
|
||||
|
||||
@Rule
|
||||
public DebuggableTemporaryFolder folder = new DebuggableTemporaryFolder();
|
||||
|
||||
@Test
|
||||
public void whenCaptureRunCommaThenDotFilesAreTheSame()
|
||||
throws InterruptedException, IOException, InferException {
|
||||
String src =
|
||||
"infer/tests/codetoanalyze/cpp/frontend/global_const/global_const2.cpp";
|
||||
ClangFrontendUtils.createAndCompareCppDotFiles(folder, src);
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright (c) 2016 - 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.objcpp;
|
||||
|
||||
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 GlobalConstTest {
|
||||
|
||||
@Rule
|
||||
public DebuggableTemporaryFolder folder = new DebuggableTemporaryFolder();
|
||||
|
||||
@Test
|
||||
public void whenCaptureRunSwitchStmtThenDotFilesAreTheSame()
|
||||
throws InterruptedException, IOException, InferException {
|
||||
String switch_src =
|
||||
"infer/tests/codetoanalyze/objcpp/frontend/global_const/global_const.mm";
|
||||
|
||||
String switch_dotty =
|
||||
"infer/tests/codetoanalyze/objcpp/frontend/global_const/global_const.mm.dot";
|
||||
|
||||
ImmutableList<String> inferCmd =
|
||||
InferRunner.createObjCPPInferCommand(folder, switch_src);
|
||||
File newDotFile = InferRunner.runInferFrontend(inferCmd);
|
||||
assertThat(
|
||||
"In the capture of " + switch_src +
|
||||
" the dotty files should be the same.",
|
||||
newDotFile, dotFileEqualTo(switch_dotty));
|
||||
}
|
||||
}
|
Loading…
Reference in new issue