[C++][Frontend] Add translation for nullptr

Summary:
@public
Make c frontend understand CXXNullPtrLiteralExpr.
Note that the implementation differs from GNUNullExpr

Test Plan:
Create function that returns nullptr:
  int* getPtr() {return nullptr;}
look at specs:
  InferPrint infer-out/specs/getPtr\{831F\}.specs
  Procedure: getPtr
  int *getPtr()
  Timestamp: 1
  Status: INACTIVE
  Phase: RE_EXECUTION
  Dependency_map:
  TIME:0.002853 s TIMEOUT:N SYMOPS:10 CALLS:1,0
  ERRORS:
  --------------------------- 1 of 1 [nvisited: 1] ---------------------------
  PRE:

  POST 1 of 1:
  return = null:
  ----------------------------------------------------------------

Add test for it
master
Andrzej Kotulski 10 years ago
parent 119521bd83
commit 819227d97d

@ -189,6 +189,11 @@ struct
let exp = Sil.Const (Sil.Cint (Sil.Int.zero)) in let exp = Sil.Const (Sil.Cint (Sil.Int.zero)) in
{ empty_res_trans with exps = [(exp, typ)]} { empty_res_trans with exps = [(exp, typ)]}
let nullPtrExpr_trans trans_state stmt_info expr_info =
Printing.log_out "Passing from nullptr '%s'\n" stmt_info.Clang_ast_t.si_pointer;
let typ = CTypes_decl.get_type_from_expr_info expr_info trans_state.context.tenv in
{ empty_res_trans with exps = [(Sil.exp_null, typ)]}
let objCSelectorExpr_trans trans_state stmt_info expr_info selector = let objCSelectorExpr_trans trans_state stmt_info expr_info selector =
stringLiteral_trans trans_state stmt_info expr_info selector stringLiteral_trans trans_state stmt_info expr_info selector
@ -1708,6 +1713,9 @@ struct
| GNUNullExpr(stmt_info, stmt_list, expr_info) -> | GNUNullExpr(stmt_info, stmt_list, expr_info) ->
gNUNullExpr_trans trans_state stmt_info expr_info gNUNullExpr_trans trans_state stmt_info expr_info
| CXXNullPtrLiteralExpr(stmt_info, stmt_list, expr_info) ->
nullPtrExpr_trans trans_state stmt_info expr_info
| ObjCSelectorExpr(stmt_info, stmt_list, expr_info, selector) -> | ObjCSelectorExpr(stmt_info, stmt_list, expr_info, selector) ->
objCSelectorExpr_trans trans_state stmt_info expr_info selector objCSelectorExpr_trans trans_state stmt_info expr_info selector

@ -0,0 +1,13 @@
digraph iCFG {
3 [label="3: Return Stmt \n *&return:int *=null [line 1]\n APPLY_ABSTRACTION; [line 1]\n " shape="box"]
3 -> 2 ;
2 [label="2: Exit getPtr \n " color=yellow style=filled]
1 [label="1: Start getPtr\nFormals: \nLocals: \n DECLARE_LOCALS(&return); [line 1]\n " color=yellow style=filled]
1 -> 3 ;
}

@ -0,0 +1,47 @@
/*
* Copyright (c) 2013- Facebook.
* All rights reserved.
*/
package frontend.cpp;
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 LiteralsTest {
@Rule
public DebuggableTemporaryFolder folder = new DebuggableTemporaryFolder();
@Test
public void whenCaptureRunCommaThenDotFilesAreTheSame()
throws InterruptedException, IOException, InferException {
String literal_src =
"infer/tests/codetoanalyze/cpp/frontend/literals/nullptr.cpp";
String literal_dotty =
"infer/tests/codetoanalyze/cpp/frontend/literals/nullptr.dot";
ImmutableList<String> inferCmd =
InferRunner.createCPPInferCommandFrontend(
folder,
literal_src);
File newDotFile = InferRunner.runInferFrontend(inferCmd);
assertThat(
"In the capture of " + literal_src +
" the dotty files should be the same.",
newDotFile, dotFileEqualTo(literal_dotty));
}
}

@ -166,6 +166,16 @@ public class InferRunner {
return langOption; return langOption;
} }
public static String getStdParam(Language lang) {
String stdParam = "";
switch (lang) {
case CPP:
stdParam = "-std=c++11";
break;
}
return stdParam;
}
public static ImmutableList<String> createClangCommand( public static ImmutableList<String> createClangCommand(
String sourceFile, String sourceFile,
Language lang, Language lang,
@ -188,6 +198,7 @@ public class InferRunner {
.add("clang") .add("clang")
.add("-x") .add("-x")
.add(getClangLangOption(lang)) .add(getClangLangOption(lang))
.add(getStdParam(lang))
.addAll(isysrootOption.build()) .addAll(isysrootOption.build())
.addAll(arcOption.build()) .addAll(arcOption.build())
.add("-c") .add("-c")

Loading…
Cancel
Save