Make generation of #line directives optional.
authormjsousa
Sun, 16 Mar 2014 13:02:28 +0000
changeset 878 89eb85bab58f
parent 877 c25346eac788
child 879 c61b2e370181
Make generation of #line directives optional.
main.cc
stage4/generate_c/generate_c.cc
stage4/generate_c/generate_c_base.cc
stage4/generate_iec/generate_iec.cc
stage4/stage4.hh
--- a/main.cc	Sun Mar 16 10:16:25 2014 +0000
+++ b/main.cc	Sun Mar 16 13:02:28 2014 +0000
@@ -109,10 +109,10 @@
 
 
 static void printusage(const char *cmd) {
-  printf("\nsyntax: %s [-h] [-v] [-f] [-s] [-c] [-I <include_directory>] [-T <target_directory>] <input_file>\n", cmd);
-  printf("  h : show this help message\n");
-  printf("  v : print version number\n");  
-  printf("  f : display full token location on error messages\n");
+  printf("\nsyntax: %s [<options>] [-O <output_options>] [-I <include_directory>] [-T <target_directory>] <input_file>\n", cmd);
+  printf(" -h : show this help message\n");
+  printf(" -v : print version number\n");  
+  printf(" -f : display full token location on error messages\n");
       /******************************************************/
       /* whether we are supporting safe extensions          */
       /* as defined in PLCopen - Technical Committee 5      */
@@ -120,10 +120,12 @@
       /* Part 1: Concepts and Function Blocks,              */
       /* Version 1.0 is Official Release                    */
       /******************************************************/
-  printf("  s : allow use of safe extensions (e.g. SAFExxx data types))\n");
-  printf("  n : allow use of nested comments (an IEC 61131-3 v3 feature)\n");
-  printf("  r : allow use of REF() operator  (an IEC 61131-3 v3 feature)\n");
-  printf("  c : create conversion functions for enumerated data types\n");
+  printf(" -s : allow use of safe extensions (e.g. SAFExxx data types))\n");
+  printf(" -n : allow use of nested comments (an IEC 61131-3 v3 feature)\n");
+  printf(" -r : allow use of REF() operator  (an IEC 61131-3 v3 feature)\n");
+  printf(" -c : create conversion functions for enumerated data types\n");
+  printf(" -O : options for output (code generation) stage. Available options for %s are...\n", cmd);
+  stage4_print_options();
   printf("\n");
   printf("%s - Copyright (C) 2003-2014 \n"
          "This program comes with ABSOLUTELY NO WARRANTY!\n"
@@ -150,15 +152,14 @@
   /******************************************/
   /*   Parse command line options...        */
   /******************************************/
-  while ((optres = getopt(argc, argv, ":nhvfrscI:T:")) != -1) {
+  while ((optres = getopt(argc, argv, ":nhvfrscI:T:O:")) != -1) {
     switch(optres) {
     case 'h':
       printusage(argv[0]);
       return 0;
 
     case 'v':
-      fprintf(stdout, "%s version %s\n"
-		      "changeset id: %s\n", PACKAGE_NAME, PACKAGE_VERSION, HGVERSION);      
+      fprintf(stdout, "%s version %s\n" "changeset id: %s\n", PACKAGE_NAME, PACKAGE_VERSION, HGVERSION);      
       return 0;
 
     case 'f':
@@ -199,7 +200,11 @@
       builddir = optarg;
       break;
 
-    case ':':       /* -I or -T without operand */
+    case 'O':
+      if (stage4_parse_options(optarg) < 0) errflg++;
+      break;
+
+    case ':':       /* -I, -T, or -O without operand */
       fprintf(stderr, "Option -%c requires an operand\n", optopt);
       errflg++;
       break;
--- a/stage4/generate_c/generate_c.cc	Sun Mar 16 10:16:25 2014 +0000
+++ b/stage4/generate_c/generate_c.cc	Sun Mar 16 13:02:28 2014 +0000
@@ -170,6 +170,41 @@
 /***********************************************************************/
 /***********************************************************************/
 
+/* Parse command line options passed from main.c !! */
+#include <stdlib.h> // for getsybopt()
+
+static int generate_line_directives__ = 0;
+
+int  stage4_parse_options(char *options) {
+  enum {                    LINE_OPT = 0             /*, SOME_OTHER_OPT, YET_ANOTHER_OPT */};
+  char *const token[] = { /*[LINE_OPT]=*/(char *)"l" /*, SOME_OTHER_OPT, ...             */, NULL };
+  /* unfortunately, the above commented out syntax for array initialization is valid in C, but not in C++ */
+  
+  char *subopts = options;
+  char *value;
+  int opt;
+
+  while (*subopts != '\0') {
+    switch (getsubopt(&subopts, token, &value)) {
+      case LINE_OPT: generate_line_directives__  = 1; break;
+      default      : fprintf(stderr, "Unrecognized option: -O %s\n", value); return -1; break;
+     }
+  }     
+  return 0;
+}
+
+
+void stage4_print_options(void) {
+  printf("          (options must be separated by commas. Example: 'l,w,x')\n"); 
+  printf("      l : insert '#line' directives in generated C code.\n"); 
+}
+
+
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+
 #include "generate_c_base.cc"
 #include "generate_c_typedecl.cc"
 #include "generate_c_sfcdecl.cc"
--- a/stage4/generate_c/generate_c_base.cc	Sun Mar 16 10:16:25 2014 +0000
+++ b/stage4/generate_c/generate_c_base.cc	Sun Mar 16 13:02:28 2014 +0000
@@ -91,6 +91,7 @@
     }
 
     void print_line_directive(symbol_c *symbol) {
+      if (!generate_line_directives__) return; /* global variable generate_line_directives__ is defined in generate_c.cc */
       s4o.print("#line ");
       s4o.print(symbol->first_line);
       s4o.print(" \"");
--- a/stage4/generate_iec/generate_iec.cc	Sun Mar 16 10:16:25 2014 +0000
+++ b/stage4/generate_iec/generate_iec.cc	Sun Mar 16 13:02:28 2014 +0000
@@ -61,8 +61,23 @@
 
 
 
-
-
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+
+/* Parse command line options passed from main.c !! */
+
+int  stage4_parse_options(char *options) {return 0;}
+
+void stage4_print_options(void) {
+  printf("          (no options available when generating IEC 61131-3 code)\n"); 
+}
+
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
 
 
 //class generate_iec_c: public iterator_visitor_c {
--- a/stage4/stage4.hh	Sun Mar 16 10:16:25 2014 +0000
+++ b/stage4/stage4.hh	Sun Mar 16 13:02:28 2014 +0000
@@ -104,4 +104,8 @@
 
 int stage4(symbol_c *tree_root, const char *builddir);
 
+/* Functions to be implemented by each generate_XX version of stage 4 */
+int  stage4_parse_options(char *options);
+void stage4_print_options(void);
+
 #endif /* _STAGE4_HH */