From 9772b2299da0a71121c35014b7b570c25a5072ac Mon Sep 17 00:00:00 2001 From: Sam Blackshear Date: Thu, 31 Aug 2017 15:52:12 -0700 Subject: [PATCH] [clang] translate user-defined literals Summary: We used to crash whenever we hit these. The simple translation implemented here is not particularly inspiring, but it is better than crashing. Reviewed By: jvillard Differential Revision: D5702095 fbshipit-source-id: 3795d43 --- infer/src/backend/dotty.ml | 8 +++--- infer/src/clang/cTrans.ml | 1 + .../cpp/frontend/literals/user_defined.cpp | 12 +++++++++ .../frontend/literals/user_defined.cpp.dot | 25 +++++++++++++++++++ 4 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 infer/tests/codetoanalyze/cpp/frontend/literals/user_defined.cpp create mode 100644 infer/tests/codetoanalyze/cpp/frontend/literals/user_defined.cpp.dot diff --git a/infer/src/backend/dotty.ml b/infer/src/backend/dotty.ml index ad46ecb7c..ac45336ab 100644 --- a/infer/src/backend/dotty.ml +++ b/infer/src/backend/dotty.ml @@ -1089,7 +1089,9 @@ let pp_proplist_parsed2dotty_file filename plist = (* channel. You have to compute an interprocedural cfg first *) let pp_cfgnodename pname fmt (n: Procdesc.Node.t) = - F.fprintf fmt "\"%s_%d\"" (Typ.Procname.to_filename pname) (Procdesc.Node.get_id n :> int) + F.fprintf fmt "\"%s_%d\"" + (Escape.escape_dotty (Typ.Procname.to_filename pname)) + (Procdesc.Node.get_id n :> int) let pp_etlist byvals fmt etl = List.iteri @@ -1110,7 +1112,7 @@ let pp_cfgnodelabel pdesc fmt (n: Procdesc.Node.t) = let pp_label fmt n = match Procdesc.Node.get_kind n with | Procdesc.Node.Start_node pname - -> let pname_string = Typ.Procname.to_string pname in + -> let pname_string = Escape.escape_dotty (Typ.Procname.to_string pname) in let attributes = Procdesc.get_attributes pdesc in let byvals = attributes.ProcAttributes.by_vals in Format.fprintf fmt "Start %s\\nFormals: %a\\nLocals: %a" pname_string (pp_etlist byvals) @@ -1121,7 +1123,7 @@ let pp_cfgnodelabel pdesc fmt (n: Procdesc.Node.t) = if not (Annot.Method.is_empty method_annotation) then Format.fprintf fmt "\\nAnnotation: %a" (Annot.Method.pp pname_string) method_annotation | Procdesc.Node.Exit_node pname - -> Format.fprintf fmt "Exit %s" (Typ.Procname.to_string pname) + -> Format.fprintf fmt "Exit %s" (Escape.escape_dotty (Typ.Procname.to_string pname)) | Procdesc.Node.Join_node -> Format.fprintf fmt "+" | Procdesc.Node.Prune_node (is_true_branch, _, _) diff --git a/infer/src/clang/cTrans.ml b/infer/src/clang/cTrans.ml index 7a1fa50e7..ffdfb5cc3 100644 --- a/infer/src/clang/cTrans.ml +++ b/infer/src/clang/cTrans.ml @@ -3127,6 +3127,7 @@ module CTrans_funct (F : CModule_type.CFrontend) : CModule_type.CTranslation = s sub-expressions *) | ExtVectorElementExpr (_, stmts, _) | ShuffleVectorExpr (_, stmts, _) + | UserDefinedLiteral (_, stmts, _) -> skip_unimplemented trans_state stmts (* Infer somehow ended up in templated non instantiated code - right now it's not supported and failure in those cases is expected. *) diff --git a/infer/tests/codetoanalyze/cpp/frontend/literals/user_defined.cpp b/infer/tests/codetoanalyze/cpp/frontend/literals/user_defined.cpp new file mode 100644 index 000000000..3e1191c04 --- /dev/null +++ b/infer/tests/codetoanalyze/cpp/frontend/literals/user_defined.cpp @@ -0,0 +1,12 @@ +/* + * 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. + */ + +int operator"" _literal(unsigned long long i) { return i; } + +int foo() { return 0_literal; } diff --git a/infer/tests/codetoanalyze/cpp/frontend/literals/user_defined.cpp.dot b/infer/tests/codetoanalyze/cpp/frontend/literals/user_defined.cpp.dot new file mode 100644 index 000000000..08067384c --- /dev/null +++ b/infer/tests/codetoanalyze/cpp/frontend/literals/user_defined.cpp.dot @@ -0,0 +1,25 @@ +/* @generated */ +digraph iCFG { +"foo#_Z3foov.69dc85dbfdb3da90badccdb11670a4ca_1" [label="1: Start foo\nFormals: \nLocals: \n DECLARE_LOCALS(&return); [line 12]\n " color=yellow style=filled] + + + "foo#_Z3foov.69dc85dbfdb3da90badccdb11670a4ca_1" -> "foo#_Z3foov.69dc85dbfdb3da90badccdb11670a4ca_3" ; +"foo#_Z3foov.69dc85dbfdb3da90badccdb11670a4ca_2" [label="2: Exit foo \n " color=yellow style=filled] + + +"foo#_Z3foov.69dc85dbfdb3da90badccdb11670a4ca_3" [label="3: Return Stmt \n *&return:int=-1 [line 12]\n " shape="box"] + + + "foo#_Z3foov.69dc85dbfdb3da90badccdb11670a4ca_3" -> "foo#_Z3foov.69dc85dbfdb3da90badccdb11670a4ca_2" ; +"operator\"\"_literal#_Zli8_literaly.5b4cb9a2f3419057428a12f4b4e6b72b_1" [label="1: Start operator\"\"_literal\nFormals: i:unsigned long long\nLocals: \n DECLARE_LOCALS(&return); [line 10]\n " color=yellow style=filled] + + + "operator\"\"_literal#_Zli8_literaly.5b4cb9a2f3419057428a12f4b4e6b72b_1" -> "operator\"\"_literal#_Zli8_literaly.5b4cb9a2f3419057428a12f4b4e6b72b_3" ; +"operator\"\"_literal#_Zli8_literaly.5b4cb9a2f3419057428a12f4b4e6b72b_2" [label="2: Exit operator\"\"_literal \n " color=yellow style=filled] + + +"operator\"\"_literal#_Zli8_literaly.5b4cb9a2f3419057428a12f4b4e6b72b_3" [label="3: Return Stmt \n n$0=*&i:unsigned long long [line 10]\n *&return:int=n$0 [line 10]\n " shape="box"] + + + "operator\"\"_literal#_Zli8_literaly.5b4cb9a2f3419057428a12f4b4e6b72b_3" -> "operator\"\"_literal#_Zli8_literaly.5b4cb9a2f3419057428a12f4b4e6b72b_2" ; +}