cprover
ms_cl_mode.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Visual Studio CL Mode
4 
5 Author: CM Wintersteiger, 2006
6 
7 \*******************************************************************/
8 
11 
12 #include "ms_cl_mode.h"
13 
14 #ifdef _WIN32
15 #define EX_OK 0
16 #define EX_USAGE 64
17 #define EX_SOFTWARE 70
18 #else
19 #include <sysexits.h>
20 #endif
21 
22 #include <iostream>
23 
24 #include <util/config.h>
25 #include <util/file_util.h>
26 #include <util/get_base_name.h>
27 #include <util/message.h>
28 #include <util/prefix.h>
29 
30 #include "compile.h"
31 #include "ms_cl_version.h"
32 
33 static bool has_directory_suffix(const std::string &path)
34 {
35  // MS CL decides whether a parameter is a directory on the
36  // basis of the / or \\ suffix; it doesn't matter
37  // whether the directory actually exists.
38  return path.empty() ? false :
39  path.back()=='/' || path.back()=='\\';
40 }
41 
44 {
45  if(cmdline.isset('?') ||
46  cmdline.isset("help"))
47  {
48  help();
49  return EX_OK;
50  }
51 
52  compilet compiler(cmdline, message_handler, cmdline.isset("WX"));
53 
54  #if 0
55  bool act_as_ld=
56  has_prefix(base_name, "link") ||
57  has_prefix(base_name, "goto-link");
58  #endif
59 
60  const auto verbosity = eval_verbosity(
62 
63  ms_cl_versiont ms_cl_version;
64  ms_cl_version.get("cl.exe");
65 
66  debug() << "Visual Studio mode " << ms_cl_version << eom;
67 
68  // model validation
69  compiler.validate_goto_model = cmdline.isset("validate-goto-model");
70 
71  // get configuration
73 
74  if(ms_cl_version.target == ms_cl_versiont::targett::x86)
76  else if(ms_cl_version.target == ms_cl_versiont::targett::ARM)
78  else if(ms_cl_version.target == ms_cl_versiont::targett::x86)
80 
82  compiler.object_file_extension="obj";
83 
84  // determine actions to be undertaken
85 
86  if(cmdline.isset('E') || cmdline.isset('P'))
88  else if(cmdline.isset('c'))
90  else
92 
93  if(cmdline.isset("std"))
94  {
95  const std::string std_string = cmdline.get_value("std");
96 
97  if(
98  std_string == ":c++14" || std_string == "=c++14" ||
99  std_string == ":c++17" || std_string == "=c++17" ||
100  std_string == ":c++latest" || std_string == "=c++latest")
101  {
102  // we don't have any newer version at the moment
103  config.cpp.set_cpp14();
104  }
105  else if(std_string == ":c++11" || std_string == "=c++11")
106  {
107  // this isn't really a Visual Studio variant, we just do this for GCC
108  // command-line compatibility
109  config.cpp.set_cpp11();
110  }
111  else
112  warning() << "unknown language standard " << std_string << eom;
113  }
114  else
115  config.cpp.set_cpp14();
116 
117  compiler.echo_file_name=true;
118 
119  if(cmdline.isset("Fo"))
120  {
121  std::string Fo_value = cmdline.get_value("Fo");
122 
123  // this could be a directory or a file name
124  if(has_directory_suffix(Fo_value))
125  {
126  compiler.output_directory_object = Fo_value;
127 
128  if(!is_directory(Fo_value))
129  warning() << "not a directory: " << Fo_value << eom;
130  }
131  else
132  compiler.output_file_object = Fo_value;
133  }
134 
135  if(
136  compiler.mode == compilet::COMPILE_ONLY &&
137  cmdline.args.size() > 1 &&
138  compiler.output_directory_object.empty())
139  {
140  error() << "output directory required for /c with multiple input files"
141  << eom;
142  return EX_USAGE;
143  }
144 
145  if(cmdline.isset("Fe"))
146  {
148 
149  // this could be a directory
150  if(
152  cmdline.args.size() >= 1)
153  {
154  if(!is_directory(compiler.output_file_executable))
155  warning() << "not a directory: "
156  << compiler.output_file_executable << eom;
157 
158  compiler.output_file_executable+=
159  get_base_name(cmdline.args[0], true) + ".exe";
160  }
161  }
162  else
163  {
164  // We need at least one argument.
165  // CL uses the first file name it gets!
166  if(cmdline.args.size()>=1)
167  compiler.output_file_executable=
168  get_base_name(cmdline.args[0], true)+".exe";
169  }
170 
171  if(cmdline.isset('J'))
173 
174  if(verbosity > messaget::M_STATISTICS)
175  {
176  std::list<std::string>::iterator it;
177 
178  std::cout << "Defines:\n";
179  for(it=config.ansi_c.defines.begin();
180  it!=config.ansi_c.defines.end();
181  it++)
182  {
183  std::cout << " " << (*it) << '\n';
184  }
185 
186  std::cout << "Undefines:\n";
187  for(it=config.ansi_c.undefines.begin();
188  it!=config.ansi_c.undefines.end();
189  it++)
190  {
191  std::cout << " " << (*it) << '\n';
192  }
193 
194  std::cout << "Preprocessor Options:\n";
195  for(it=config.ansi_c.preprocessor_options.begin();
197  it++)
198  {
199  std::cout << " " << (*it) << '\n';
200  }
201 
202  std::cout << "Include Paths:\n";
203  for(it=config.ansi_c.include_paths.begin();
204  it!=config.ansi_c.include_paths.end();
205  it++)
206  {
207  std::cout << " " << (*it) << '\n';
208  }
209 
210  std::cout << "Library Paths:\n";
211  for(it=compiler.library_paths.begin();
212  it!=compiler.library_paths.end();
213  it++)
214  {
215  std::cout << " " << (*it) << '\n';
216  }
217 
218  std::cout << "Output file (object): "
219  << compiler.output_file_object << '\n';
220  std::cout << "Output file (executable): "
221  << compiler.output_file_executable << '\n';
222  }
223 
224  // Parse input program, convert to goto program, write output
225  return compiler.doit() ? EX_USAGE : EX_OK;
226 }
227 
230 {
231  std::cout << "goto-cl understands the options of CL plus the following.\n\n";
232 }
cmdlinet::args
argst args
Definition: cmdline.h:91
ms_cl_modet::help_mode
virtual void help_mode()
display command line help
Definition: ms_cl_mode.cpp:229
get_base_name
std::string get_base_name(const std::string &in, bool strip_suffix)
cleans a filename from path and extension
Definition: get_base_name.cpp:16
configt::ansi_ct::defines
std::list< std::string > defines
Definition: config.h:122
compilet::echo_file_name
bool echo_file_name
Definition: compile.h:34
cmdlinet::isset
virtual bool isset(char option) const
Definition: cmdline.cpp:29
file_util.h
configt::ansi_ct::include_paths
std::list< std::string > include_paths
Definition: config.h:125
messaget::M_STATISTICS
@ M_STATISTICS
Definition: message.h:171
configt::ansi_ct::flavourt::VISUAL_STUDIO
@ VISUAL_STUDIO
configt::ansi_ct::set_32
void set_32()
Definition: config.cpp:31
prefix.h
configt::ansi_ct::set_64
void set_64()
Definition: config.cpp:36
compilet::doit
bool doit()
reads and source and object files, compiles and links them into goto program objects.
Definition: compile.cpp:57
goto_cc_modet::base_name
const std::string base_name
Definition: goto_cc_mode.h:38
ms_cl_modet::message_handler
cl_message_handlert message_handler
Definition: ms_cl_mode.h:37
messaget::eom
static eomt eom
Definition: message.h:297
configt::ansi_ct::undefines
std::list< std::string > undefines
Definition: config.h:123
configt::ansi_c
struct configt::ansi_ct ansi_c
compilet::validate_goto_model
bool validate_goto_model
Definition: compile.h:37
ms_cl_modet::doit
virtual int doit()
does it.
Definition: ms_cl_mode.cpp:43
configt::cppt::set_cpp14
void set_cpp14()
Definition: config.h:154
compilet::output_file_object
std::string output_file_object
Definition: compile.h:58
messaget::error
mstreamt & error() const
Definition: message.h:399
compilet::mode
enum compilet::@2 mode
compilet::output_file_executable
std::string output_file_executable
Definition: compile.h:55
configt::cppt::set_cpp11
void set_cpp11()
Definition: config.h:153
messaget::M_ERROR
@ M_ERROR
Definition: message.h:170
cmdlinet::get_value
std::string get_value(char option) const
Definition: cmdline.cpp:47
compilet::library_paths
std::list< std::string > library_paths
Definition: compile.h:47
compile.h
Compile and link source and object files.
is_directory
bool is_directory(const std::string &path)
Definition: file_util.cpp:187
compilet::object_file_extension
std::string object_file_extension
Definition: compile.h:54
get_base_name.h
compilet::COMPILE_LINK_EXECUTABLE
@ COMPILE_LINK_EXECUTABLE
Definition: compile.h:44
configt::ansi_ct::preprocessor_options
std::list< std::string > preprocessor_options
Definition: config.h:124
ms_cl_versiont::targett::ARM
@ ARM
compilet::PREPROCESS_ONLY
@ PREPROCESS_ONLY
Definition: compile.h:39
compilet::output_directory_object
std::string output_directory_object
Definition: compile.h:58
config
configt config
Definition: config.cpp:24
ms_cl_versiont
Definition: ms_cl_version.h:20
configt::ansi_ct::mode
flavourt mode
Definition: config.h:116
ms_cl_versiont::targett::x86
@ x86
configt::set
bool set(const cmdlinet &cmdline)
Definition: config.cpp:798
has_directory_suffix
static bool has_directory_suffix(const std::string &path)
Definition: ms_cl_mode.cpp:33
ms_cl_modet::cmdline
ms_cl_cmdlinet & cmdline
Definition: ms_cl_mode.h:36
ms_cl_version.h
config.h
configt::ansi_ct::char_is_unsigned
bool char_is_unsigned
Definition: config.h:44
has_prefix
bool has_prefix(const std::string &s, const std::string &prefix)
Definition: converter.cpp:13
messaget::debug
mstreamt & debug() const
Definition: message.h:429
goto_cc_modet::help
void help()
display command line help
Definition: goto_cc_mode.cpp:47
configt::cpp
struct configt::cppt cpp
compilet
Definition: compile.h:27
messaget::eval_verbosity
static unsigned eval_verbosity(const std::string &user_input, const message_levelt default_verbosity, message_handlert &dest)
Parse a (user-)provided string as a verbosity level and set it as the verbosity of dest.
Definition: message.cpp:104
message.h
compilet::COMPILE_ONLY
@ COMPILE_ONLY
Definition: compile.h:40
messaget::warning
mstreamt & warning() const
Definition: message.h:404
ms_cl_mode.h
Visual Studio CL Mode.
ms_cl_versiont::get
void get(const std::string &executable)
Definition: ms_cl_version.cpp:18
ms_cl_versiont::target
enum ms_cl_versiont::targett target