LCOV - code coverage report
Current view: top level - root/contrail/src/contrail-common/sandesh/compiler - main.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 431 740 58.2 %
Date: 2026-11-09 02:10:49 Functions: 18 24 75.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Licensed to the Apache Software Foundation (ASF) under one
       3             :  * or more contributor license agreements. See the NOTICE file
       4             :  * distributed with this work for additional information
       5             :  * regarding copyright ownership. The ASF licenses this file
       6             :  * to you under the Apache License, Version 2.0 (the
       7             :  * "License"); you may not use this file except in compliance
       8             :  * with the License. You may obtain a copy of the License at
       9             :  *
      10             :  *   http://www.apache.org/licenses/LICENSE-2.0
      11             :  *
      12             :  * Unless required by applicable law or agreed to in writing,
      13             :  * software distributed under the License is distributed on an
      14             :  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
      15             :  * KIND, either express or implied. See the License for the
      16             :  * specific language governing permissions and limitations
      17             :  * under the License.
      18             :  *
      19             :  * Copyright 2006-2017 The Apache Software Foundation.
      20             :  * https://github.com/apache/thrift
      21             :  *
      22             :  * Modifications Copyright (c) Juniper Networks, Inc.
      23             :  * Copyright Contributors to the OpenSDN project
      24             :  */
      25             : 
      26             : /**
      27             :  * sandesh - a lightweight cross-language message generator
      28             :  * based on thrift
      29             :  *
      30             :  * This file contains the main compiler engine for sandesh,
      31             :  * which invokes the scanner/parser to build the thrift object tree.
      32             :  * The interface generation code for each language lives in a file
      33             :  * by the language name under the generate/ folder, and all parse
      34             :  * structures live in parse/
      35             :  *
      36             :  */
      37             : 
      38             : #include <cassert>
      39             : #include <stdlib.h>
      40             : #include <stdio.h>
      41             : #include <stdarg.h>
      42             : #include <time.h>
      43             : #include <string>
      44             : #include <algorithm>
      45             : #include <sys/types.h>
      46             : #include <sys/stat.h>
      47             : #include <errno.h>
      48             : #include <limits.h>
      49             : 
      50             : // Careful: must include globals first for extern definitions
      51             : #include "globals.h"
      52             : 
      53             : #include "main.h"
      54             : #include "parse/t_program.h"
      55             : #include "parse/t_scope.h"
      56             : #include "generate/t_generator.h"
      57             : 
      58             : #include "version.h"
      59             : 
      60             : using namespace std;
      61             : 
      62             : /**
      63             :  * Global program tree
      64             :  */
      65             : t_program* g_program;
      66             : 
      67             : /**
      68             :  * Global types
      69             :  */
      70             : 
      71             : t_type* g_type_void;
      72             : t_type* g_type_string;
      73             : t_type* g_type_binary;
      74             : t_type* g_type_slist;
      75             : t_type* g_type_bool;
      76             : t_type* g_type_byte;
      77             : t_type* g_type_i16;
      78             : t_type* g_type_i32;
      79             : t_type* g_type_i64;
      80             : t_type* g_type_double;
      81             : #ifdef SANDESH
      82             : t_type* g_type_u16;
      83             : t_type* g_type_u32;
      84             : t_type* g_type_u64;
      85             : t_type* g_type_ipv4;
      86             : t_type* g_type_ipaddr;
      87             : t_type* g_type_static_const_string;
      88             : t_type* g_type_sandesh_system;
      89             : t_type* g_type_sandesh_request;
      90             : t_type* g_type_sandesh_response;
      91             : t_type* g_type_sandesh_trace;
      92             : t_type* g_type_sandesh_trace_object;
      93             : t_type* g_type_sandesh_buffer;
      94             : t_type* g_type_sandesh_uve;
      95             : t_type* g_type_sandesh_dynamic_uve;
      96             : t_type* g_type_sandesh_alarm;
      97             : t_type* g_type_sandesh_object;
      98             : t_type* g_type_sandesh_flow;
      99             : t_type* g_type_sandesh_session;
     100             : t_type* g_type_xml;
     101             : t_type* g_type_uuid_t;
     102             : 
     103             : #endif
     104             : 
     105             : 
     106             : /**
     107             :  * Global scope
     108             :  */
     109             : t_scope* g_scope;
     110             : 
     111             : /**
     112             :  * Parent scope to also parse types
     113             :  */
     114             : t_scope* g_parent_scope;
     115             : 
     116             : /**
     117             :  * Prefix for putting types in parent scope
     118             :  */
     119             : string g_parent_prefix;
     120             : 
     121             : /**
     122             :  * Parsing pass
     123             :  */
     124             : PARSE_MODE g_parse_mode;
     125             : 
     126             : /**
     127             :  * Current directory of file being parsed
     128             :  */
     129             : string g_curdir;
     130             : 
     131             : /**
     132             :  * Current file being parsed
     133             :  */
     134             : string g_curpath;
     135             : 
     136             : /**
     137             :  * Search path for inclusions
     138             :  */
     139             : vector<string> g_incl_searchpath;
     140             : 
     141             : /**
     142             :  * Should C++ include statements use path prefixes for other thrift-generated
     143             :  * header files
     144             :  */
     145             : bool g_cpp_use_include_prefix = false;
     146             : 
     147             : /**
     148             :  * Global debug state
     149             :  */
     150             : int g_debug = 0;
     151             : 
     152             : /**
     153             :  * Strictness level
     154             :  */
     155             : int g_strict = 127;
     156             : 
     157             : /**
     158             :  * Warning level
     159             :  */
     160             : int g_warn = 1;
     161             : 
     162             : /**
     163             :  * Verbose output
     164             :  */
     165             : int g_verbose = 0;
     166             : 
     167             : /**
     168             :  * Global time string
     169             :  */
     170             : char* g_time_str;
     171             : 
     172             : /**
     173             :  * The last parsed doctext comment.
     174             :  */
     175             : char* g_doctext;
     176             : 
     177             : /**
     178             :  * The location of the last parsed doctext comment.
     179             :  */
     180             : int g_doctext_lineno;
     181             : 
     182             : /**
     183             :  * The First doctext comment
     184             :  */
     185             : char* g_program_doctext_candidate;
     186             : int  g_program_doctext_lineno = 0;
     187             : PROGDOCTEXT_STATUS  g_program_doctext_status = INVALID;
     188             : 
     189             : /**
     190             :  * Whether or not negative field keys are accepted.
     191             :  */
     192             : int g_allow_neg_field_keys;
     193             : 
     194             : /**
     195             :  * Whether or not 64-bit constants will generate a warning.
     196             :  */
     197             : int g_allow_64bit_consts = 0;
     198             : 
     199             : /**
     200             :  * Flags to control code generation
     201             :  */
     202             : bool gen_cpp = false;
     203             : bool gen_dense = false;
     204             : bool gen_java = false;
     205             : bool gen_javabean = false;
     206             : bool gen_rb = false;
     207             : bool gen_py = false;
     208             : bool gen_py_newstyle = false;
     209             : bool gen_xsd = false;
     210             : bool gen_php = false;
     211             : bool gen_phpi = false;
     212             : bool gen_phps = true;
     213             : bool gen_phpa = false;
     214             : bool gen_phpo = false;
     215             : bool gen_rest = false;
     216             : bool gen_perl = false;
     217             : bool gen_erl = false;
     218             : bool gen_ocaml = false;
     219             : bool gen_hs = false;
     220             : bool gen_cocoa = false;
     221             : bool gen_csharp = false;
     222             : bool gen_delphi = false;
     223             : bool gen_st = false;
     224             : bool gen_recurse = false;
     225             : 
     226             : /**
     227             :  * MinGW doesn't have realpath, so use fallback implementation in that case,
     228             :  * otherwise this just calls through to realpath
     229             :  */
     230        1144 : char *saferealpath(const char *path, char *resolved_path) {
     231        1144 :   return realpath(path, resolved_path);
     232             : }
     233             : 
     234             : 
     235             : /**
     236             :  * Report an error to the user. This is called yyerror for historical
     237             :  * reasons (lex and yacc expect the error reporting routine to be called
     238             :  * this). Call this function to report any errors to the user.
     239             :  * yyerror takes printf style arguments.
     240             :  *
     241             :  * @param fmt C format string followed by additional arguments
     242             :  */
     243           0 : void yyerror(const char* fmt, ...) {
     244             :   va_list args;
     245           0 :   fprintf(stderr,
     246             :           "[ERROR:%s:%d] (last token was '%s')\n",
     247             :           g_curpath.c_str(),
     248             :           yylineno,
     249             :           yytext);
     250             : 
     251           0 :   va_start(args, fmt);
     252           0 :   vfprintf(stderr, fmt, args);
     253           0 :   va_end(args);
     254             : 
     255           0 :   fprintf(stderr, "\n");
     256           0 : }
     257             : 
     258             : /**
     259             :  * Prints a debug message from the parser.
     260             :  *
     261             :  * @param fmt C format string followed by additional arguments
     262             :  */
     263      831591 : void pdebug(const char* fmt, ...) {
     264      831591 :   if (g_debug == 0) {
     265      831591 :     return;
     266             :   }
     267             :   va_list args;
     268           0 :   printf("[PARSE:%d] ", yylineno);
     269           0 :   va_start(args, fmt);
     270           0 :   vprintf(fmt, args);
     271           0 :   va_end(args);
     272           0 :   printf("\n");
     273             : }
     274             : 
     275             : /**
     276             :  * Prints a verbose output mode message
     277             :  *
     278             :  * @param fmt C format string followed by additional arguments
     279             :  */
     280        2877 : void pverbose(const char* fmt, ...) {
     281        2877 :   if (g_verbose == 0) {
     282        2877 :     return;
     283             :   }
     284             :   va_list args;
     285           0 :   va_start(args, fmt);
     286           0 :   vprintf(fmt, args);
     287           0 :   va_end(args);
     288             : }
     289             : 
     290             : /**
     291             :  * Prints a warning message
     292             :  *
     293             :  * @param fmt C format string followed by additional arguments
     294             :  */
     295         339 : void pwarning(int level, const char* fmt, ...) {
     296         339 :   if (g_warn < level) {
     297         313 :     return;
     298             :   }
     299             :   va_list args;
     300          26 :   printf("[WARNING:%s:%d] ", g_curpath.c_str(), yylineno);
     301          26 :   va_start(args, fmt);
     302          26 :   vprintf(fmt, args);
     303          26 :   va_end(args);
     304          26 :   printf("\n");
     305             : }
     306             : 
     307             : /**
     308             :  * Prints a failure message and exits
     309             :  *
     310             :  * @param fmt C format string followed by additional arguments
     311             :  */
     312           0 : void failure(const char* fmt, ...) {
     313             :   va_list args;
     314           0 :   fprintf(stderr, "[FAILURE:%s:%d] ", g_curpath.c_str(), yylineno);
     315           0 :   va_start(args, fmt);
     316           0 :   vfprintf(stderr, fmt, args);
     317           0 :   va_end(args);
     318           0 :   printf("\n");
     319           0 :   exit(1);
     320             : }
     321             : 
     322             : /**
     323             :  * Converts a string filename into a thrift program name
     324             :  */
     325         776 : string program_name(string filename) {
     326         776 :   string::size_type slash = filename.rfind("/");
     327         776 :   if (slash != string::npos) {
     328         776 :     filename = filename.substr(slash+1);
     329             :   }
     330         776 :   string::size_type dot = filename.rfind(".");
     331         776 :   if (dot != string::npos) {
     332         776 :     filename = filename.substr(0, dot);
     333             :   }
     334         776 :   return filename;
     335             : }
     336             : 
     337             : /**
     338             :  * Gets the directory path of a filename
     339             :  */
     340         776 : string directory_name(string filename) {
     341         776 :   string::size_type slash = filename.rfind("/");
     342             :   // No slash, just use the current directory
     343         776 :   if (slash == string::npos) {
     344           0 :     return ".";
     345             :   }
     346         776 :   return filename.substr(0, slash);
     347             : }
     348             : 
     349             : /**
     350             :  * Finds the appropriate file path for the given filename
     351             :  */
     352         214 : string include_file(string filename) {
     353             :   // Absolute path? Just try that
     354         214 :   if (filename[0] == '/') {
     355             :     // Realpath!
     356             :     char rp[PATH_MAX];
     357           0 :     if (saferealpath(filename.c_str(), rp) == NULL) {
     358           0 :       pwarning(0, "Cannot open include file %s\n", filename.c_str());
     359           0 :       return std::string();
     360             :     }
     361             : 
     362             :     // Stat this file
     363             :     struct stat finfo;
     364           0 :     if (stat(rp, &finfo) == 0) {
     365           0 :       return rp;
     366             :     }
     367             :   } else { // relative path, start searching
     368             :     // new search path with current dir global
     369         214 :     vector<string> sp = g_incl_searchpath;
     370         214 :     sp.insert(sp.begin(), g_curdir);
     371             : 
     372             :     // iterate through paths
     373         214 :     vector<string>::iterator it;
     374         582 :     for (it = sp.begin(); it != sp.end(); it++) {
     375         582 :       string sfilename = *(it) + "/" + filename;
     376             : 
     377             :       // Realpath!
     378             :       char rp[PATH_MAX];
     379         582 :       if (saferealpath(sfilename.c_str(), rp) == NULL) {
     380         368 :         continue;
     381             :       }
     382             : 
     383             :       // Stat this files
     384             :       struct stat finfo;
     385         214 :       if (stat(rp, &finfo) == 0) {
     386         214 :         return rp;
     387             :       }
     388         582 :     }
     389         214 :   }
     390             : 
     391             :   // Uh oh
     392           0 :   pwarning(0, "Could not find include file %s\n", filename.c_str());
     393           0 :   return std::string();
     394             : }
     395             : 
     396             : /**
     397             :  * Clears any previously stored doctext string.
     398             :  * Also prints a warning if we are discarding information.
     399             :  */
     400       19247 : void clear_doctext() {
     401       19247 :   if (g_doctext != NULL) {
     402         313 :     pwarning(2, "Uncaptured doctext at on line %d.", g_doctext_lineno);
     403             :   }
     404       19247 :   free(g_doctext);
     405       19247 :   g_doctext = NULL;
     406       19247 : }
     407             : 
     408             : /**
     409             :  * Reset program doctext information after processing a file
     410             :  */
     411         776 : void reset_program_doctext_info() {
     412         776 :   if(g_program_doctext_candidate != NULL) {
     413         181 :     free(g_program_doctext_candidate);
     414         181 :     g_program_doctext_candidate = NULL;
     415             :   }
     416         776 :   g_program_doctext_lineno = 0;
     417         776 :   g_program_doctext_status = INVALID;
     418         776 :   pdebug("%s","program doctext set to INVALID");
     419         776 : }
     420             : 
     421             : /**
     422             :  * We are sure the program doctext candidate is really the program doctext.
     423             :  */
     424           0 : void declare_valid_program_doctext() {
     425           0 :   if((g_program_doctext_candidate != NULL) && (g_program_doctext_status == STILL_CANDIDATE)) {
     426           0 :     g_program_doctext_status = ABSOLUTELY_SURE;
     427           0 :     pdebug("%s","program doctext set to ABSOLUTELY_SURE");
     428             :   } else {
     429           0 :     g_program_doctext_status = NO_PROGRAM_DOCTEXT;
     430           0 :     pdebug("%s","program doctext set to NO_PROGRAM_DOCTEXT");
     431             :   }
     432           0 : }
     433             : 
     434             : /**
     435             :  * Cleans up text commonly found in doxygen-like comments
     436             :  *
     437             :  * Warning: if you mix tabs and spaces in a non-uniform way,
     438             :  * you will get what you deserve.
     439             :  */
     440       17387 : char* clean_up_doctext(char* doctext) {
     441             :   // Convert to C++ string, and remove carriage returns if added.
     442       17387 :   string docstring = doctext;
     443       34774 :   docstring.erase(
     444       17387 :       remove(docstring.begin(), docstring.end(), '\r'),
     445       17387 :       docstring.end());
     446             : 
     447             :   // Separate into lines.
     448       17387 :   vector<string> lines;
     449       17387 :   string::size_type pos = string::npos;
     450             :   string::size_type last;
     451             :   while (true) {
     452       38195 :     last = (pos == string::npos) ? 0 : pos+1;
     453       38195 :     pos = docstring.find('\n', last);
     454       38195 :     if (pos == string::npos) {
     455             :       // First bit of cleaning.  If the last line is only whitespace, drop it.
     456       17387 :       string::size_type nonwhite = docstring.find_first_not_of(" \t", last);
     457       17387 :       if (nonwhite != string::npos) {
     458       10875 :         lines.push_back(docstring.substr(last));
     459             :       }
     460       17387 :       break;
     461             :     }
     462       20808 :     lines.push_back(docstring.substr(last, pos-last));
     463       20808 :   }
     464             : 
     465             :   // A very profound docstring.
     466       17387 :   if (lines.empty()) {
     467           0 :     return NULL;
     468             :   }
     469             : 
     470             :   // Clear leading whitespace from the first line.
     471       17387 :   pos = lines.front().find_first_not_of(" \t");
     472       17387 :   lines.front().erase(0, pos);
     473             : 
     474             :   // If every nonblank line after the first has the same number of spaces/tabs,
     475             :   // then a star, remove them.
     476       17387 :   bool have_prefix = true;
     477       17387 :   bool found_prefix = false;
     478       17387 :   string::size_type prefix_len = 0;
     479       17387 :   vector<string>::iterator l_iter;
     480       31534 :   for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
     481       14273 :     if (l_iter->empty()) {
     482           1 :       continue;
     483             :     }
     484             : 
     485       14272 :     pos = l_iter->find_first_not_of(" \t");
     486       14272 :     if (!found_prefix) {
     487        6506 :       if (pos != string::npos) {
     488        6506 :         if (l_iter->at(pos) == '*') {
     489        6482 :           found_prefix = true;
     490        6482 :           prefix_len = pos;
     491             :         } else {
     492          24 :           have_prefix = false;
     493          24 :           break;
     494             :         }
     495             :       } else {
     496             :         // Whitespace-only line.  Truncate it.
     497           0 :         l_iter->clear();
     498             :       }
     499        7766 :     } else if (l_iter->size() > pos
     500        7766 :         && l_iter->at(pos) == '*'
     501       15532 :         && pos == prefix_len) {
     502             :       // Business as usual.
     503         102 :     } else if (pos == string::npos) {
     504             :       // Whitespace-only line.  Let's truncate it for them.
     505           0 :       l_iter->clear();
     506             :     } else {
     507             :       // The pattern has been broken.
     508         102 :       have_prefix = false;
     509         102 :       break;
     510             :     }
     511             :   }
     512             : 
     513             :   // If our prefix survived, delete it from every line.
     514       17387 :   if (have_prefix) {
     515             :     // Get the star too.
     516       17261 :     prefix_len++;
     517       30971 :     for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
     518       13710 :       l_iter->erase(0, prefix_len);
     519             :     }
     520             :   }
     521             : 
     522             :   // Now delete the minimum amount of leading whitespace from each line.
     523       17387 :   prefix_len = string::npos;
     524       31683 :   for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
     525       14296 :     if (l_iter->empty()) {
     526         254 :       continue;
     527             :     }
     528       14042 :     pos = l_iter->find_first_not_of(" \t");
     529       14042 :     if (pos != string::npos
     530       14042 :         && (prefix_len == string::npos || pos < prefix_len)) {
     531        6577 :       prefix_len = pos;
     532             :     }
     533             :   }
     534             : 
     535             :   // If our prefix survived, delete it from every line.
     536       17387 :   if (prefix_len != string::npos) {
     537       20802 :     for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
     538       14296 :       l_iter->erase(0, prefix_len);
     539             :     }
     540             :   }
     541             : 
     542             :   // Remove trailing whitespace from every line.
     543       49070 :   for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
     544       31683 :     pos = l_iter->find_last_not_of(" \t");
     545       31683 :     if (pos != string::npos && pos != l_iter->length()-1) {
     546       10875 :       l_iter->erase(pos+1);
     547             :     }
     548             :   }
     549             : 
     550             :   // If the first line is empty, remove it.
     551             :   // Don't do this earlier because a lot of steps skip the first line.
     552       17387 :   if (lines.front().empty()) {
     553        6128 :     lines.erase(lines.begin());
     554             :   }
     555             : 
     556             :   // Now rejoin the lines and copy them back into doctext.
     557       17387 :   docstring.clear();
     558       42942 :   for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
     559       25555 :     docstring += *l_iter;
     560       25555 :     docstring += '\n';
     561             :   }
     562             : 
     563       17387 :   assert(docstring.length() <= strlen(doctext));
     564       17387 :   strcpy(doctext, docstring.c_str());
     565       17387 :   return doctext;
     566       17387 : }
     567             : 
     568             : /** Set to true to debug docstring parsing */
     569             : static bool dump_docs = false;
     570             : 
     571             : /**
     572             :  * Dumps docstrings to stdout
     573             :  * Only works for top-level definitions and the whole program doc
     574             :  * (i.e., not enum constants, struct fields, or functions.
     575             :  */
     576           0 : void dump_docstrings(t_program* program) {
     577             : 
     578           0 :   string progdoc = program->get_doc();
     579           0 :   if (!progdoc.empty()) {
     580           0 :     printf("Whole program doc:\n%s\n", progdoc.c_str());
     581             :   }
     582           0 :   const vector<t_typedef*>& typedefs = program->get_typedefs();
     583           0 :   vector<t_typedef*>::const_iterator t_iter;
     584           0 :   for (t_iter = typedefs.begin(); t_iter != typedefs.end(); ++t_iter) {
     585           0 :     t_typedef* td = *t_iter;
     586           0 :     if (td->has_doc()) {
     587           0 :       printf("typedef %s:\n%s\n", td->get_name().c_str(), td->get_doc().c_str());
     588             :     }
     589             :   }
     590           0 :   const vector<t_enum*>& enums = program->get_enums();
     591           0 :   vector<t_enum*>::const_iterator e_iter;
     592           0 :   for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
     593           0 :     t_enum* en = *e_iter;
     594           0 :     if (en->has_doc()) {
     595           0 :       printf("enum %s:\n%s\n", en->get_name().c_str(), en->get_doc().c_str());
     596             :     }
     597             :   }
     598           0 :   const vector<t_const*>& consts = program->get_consts();
     599           0 :   vector<t_const*>::const_iterator c_iter;
     600           0 :   for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
     601           0 :     t_const* co = *c_iter;
     602           0 :     if (co->has_doc()) {
     603           0 :       printf("const %s:\n%s\n", co->get_name().c_str(), co->get_doc().c_str());
     604             :     }
     605             :   }
     606           0 :   const vector<t_struct*>& structs = program->get_structs();
     607           0 :   vector<t_struct*>::const_iterator s_iter;
     608           0 :   for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
     609           0 :     t_struct* st = *s_iter;
     610           0 :     if (st->has_doc()) {
     611           0 :       printf("struct %s:\n%s\n", st->get_name().c_str(), st->get_doc().c_str());
     612             :     }
     613           0 :     const vector<t_field*>& fields = st->get_members();
     614           0 :     vector<t_field*>::const_iterator f_iter;
     615           0 :     for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
     616           0 :        t_field* fp  = *f_iter;
     617           0 :        if (fp->has_doc()) {
     618           0 :          printf("fields %s:\n%s\n",
     619           0 :                  fp->get_name().c_str(), fp->get_doc().c_str());
     620             :        }
     621             :     }
     622             :   }
     623           0 :   const vector<t_struct*>& xceptions = program->get_xceptions();
     624           0 :   vector<t_struct*>::const_iterator x_iter;
     625           0 :   for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
     626           0 :     t_struct* xn = *x_iter;
     627           0 :     if (xn->has_doc()) {
     628           0 :       printf("xception %s:\n%s\n", xn->get_name().c_str(), xn->get_doc().c_str());
     629             :     }
     630             :   }
     631             : #ifdef SANDESH
     632           0 :   const vector<t_sandesh*>& sandeshs = program->get_sandeshs();
     633           0 :   vector<t_sandesh*>::const_iterator sn_iter;
     634           0 :   for (sn_iter = sandeshs.begin(); sn_iter != sandeshs.end(); ++sn_iter) {
     635           0 :     t_sandesh* sn = *sn_iter;
     636           0 :     if (sn->has_doc()) {
     637           0 :       printf("sandesh %s:\n%s\n", sn->get_name().c_str(), sn->get_doc().c_str());
     638             :     }
     639           0 :     const vector<t_field*>& sn_fields = sn->get_members();
     640           0 :     vector<t_field*>::const_iterator snf_iter;
     641           0 :     for (snf_iter = sn_fields.begin(); snf_iter != sn_fields.end(); ++snf_iter) {
     642           0 :        t_field* snfp  = *snf_iter;
     643           0 :        if (snfp->has_doc()) {
     644           0 :          printf("fields %s:\n%s\n",
     645           0 :                  snfp->get_name().c_str(), snfp->get_doc().c_str());
     646             :        }
     647             :     }
     648             :   }
     649             : #endif
     650           0 :   const vector<t_service*>& services = program->get_services();
     651           0 :   vector<t_service*>::const_iterator v_iter;
     652           0 :   for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
     653           0 :     t_service* sv = *v_iter;
     654           0 :     if (sv->has_doc()) {
     655           0 :       printf("service %s:\n%s\n", sv->get_name().c_str(), sv->get_doc().c_str());
     656             :     }
     657             :   }
     658           0 : }
     659             : 
     660             : /**
     661             :  * Call generate_fingerprint for every structure and enum.
     662             :  */
     663         562 : void generate_all_fingerprints(t_program* program) {
     664         562 :   const vector<t_struct*>& structs = program->get_structs();
     665         562 :   vector<t_struct*>::const_iterator s_iter;
     666        2833 :   for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
     667        2271 :     t_struct* st = *s_iter;
     668        2271 :     st->generate_fingerprint();
     669             :   }
     670             : 
     671         562 :   const vector<t_struct*>& xceptions = program->get_xceptions();
     672         562 :   vector<t_struct*>::const_iterator x_iter;
     673         562 :   for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
     674           0 :     t_struct* st = *x_iter;
     675           0 :     st->generate_fingerprint();
     676             :   }
     677             : 
     678         562 :   const vector<t_enum*>& enums = program->get_enums();
     679         562 :   vector<t_enum*>::const_iterator e_iter;
     680         903 :   for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
     681         341 :     t_enum* e = *e_iter;
     682         341 :     e->generate_fingerprint();
     683             :   }
     684             : 
     685         562 :   g_type_void->generate_fingerprint();
     686             : 
     687             : #ifdef SANDESH
     688             :   {
     689         562 :   const vector<t_sandesh*>& sandeshs = program->get_sandeshs();
     690         562 :   vector<t_sandesh*>::const_iterator s_iter;
     691        4090 :   for (s_iter = sandeshs.begin(); s_iter != sandeshs.end(); ++s_iter) {
     692        3528 :     t_sandesh* st = *s_iter;
     693        3528 :     st->generate_fingerprint();
     694             :   }
     695             :   }
     696             : #endif
     697             : 
     698             :   // If you want to generate fingerprints for implicit structures, start here.
     699             :   /*
     700             :   const vector<t_service*>& services = program->get_services();
     701             :   vector<t_service*>::const_iterator v_iter;
     702             :   for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
     703             :     t_service* sv = *v_iter;
     704             :   }
     705             :   */
     706         562 : }
     707             : 
     708             : /**
     709             :  * Prints the version number
     710             :  */
     711         397 : void version() {
     712             : #ifdef SANDESH
     713         397 :   printf("Sandesh version %s\n", SANDESH_VERSION);
     714             : #else
     715             :   printf("Thrift version %s\n", THRIFT_VERSION);
     716             : #endif
     717         397 : }
     718             : 
     719             : /**
     720             :  * Diplays the usage message and then exits with an error code.
     721             :  */
     722           0 : void usage() {
     723             : #ifdef SANDESH
     724           0 :   fprintf(stderr, "Usage: sandesh [options] file\n");
     725             : #else
     726             :   fprintf(stderr, "Usage: thrift [options] file\n");
     727             : #endif
     728           0 :   fprintf(stderr, "Options:\n");
     729           0 :   fprintf(stderr, "  -version    Print the compiler version\n");
     730           0 :   fprintf(stderr, "  -o dir      Set the output directory for gen-* packages\n");
     731           0 :   fprintf(stderr, "               (default: current directory)\n");
     732           0 :   fprintf(stderr, "  -out dir    Set the ouput location for generated files.\n");
     733           0 :   fprintf(stderr,"               (no gen-* folder will be created)\n");
     734           0 :   fprintf(stderr, "  -I dir      Add a directory to the list of directories\n");
     735           0 :   fprintf(stderr, "                searched for include directives\n");
     736           0 :   fprintf(stderr, "  -nowarn     Suppress all compiler warnings (BAD!)\n");
     737           0 :   fprintf(stderr, "  -strict     Strict compiler warnings on\n");
     738           0 :   fprintf(stderr, "  -v[erbose]  Verbose mode\n");
     739           0 :   fprintf(stderr, "  -r[ecurse]  Also generate included files\n");
     740           0 :   fprintf(stderr, "  -debug      Parse debug trace to stdout\n");
     741           0 :   fprintf(stderr, "  --allow-neg-keys  Allow negative field keys (Used to "
     742             :           "preserve protocol\n");
     743           0 :   fprintf(stderr, "                compatibility with older .thrift files)\n");
     744           0 :   fprintf(stderr, "  --allow-64bit-consts  Do not print warnings about using 64-bit constants\n");
     745           0 :   fprintf(stderr, "  --gen STR   Generate code with a dynamically-registered generator.\n");
     746           0 :   fprintf(stderr, "                STR has the form language[:key1=val1[,key2,[key3=val3]]].\n");
     747           0 :   fprintf(stderr, "                Keys and values are options passed to the generator.\n");
     748           0 :   fprintf(stderr, "                Many options will not require values.\n");
     749           0 :   fprintf(stderr, "\n");
     750           0 :   fprintf(stderr, "Available generators (and options):\n");
     751           0 :   t_generator_registry::gen_map_t gen_map = t_generator_registry::get_generator_map();
     752           0 :   t_generator_registry::gen_map_t::iterator iter;
     753           0 :   for (iter = gen_map.begin(); iter != gen_map.end(); ++iter) {
     754           0 :     fprintf(stderr, "  %s (%s):\n",
     755           0 :         iter->second->get_short_name().c_str(),
     756           0 :         iter->second->get_long_name().c_str());
     757           0 :     fprintf(stderr, "%s", iter->second->get_documentation().c_str());
     758             :   }
     759           0 :   exit(1);
     760           0 : }
     761             : 
     762             : /**
     763             :  * You know, when I started working on Thrift I really thought it wasn't going
     764             :  * to become a programming language because it was just a generator and it
     765             :  * wouldn't need runtime type information and all that jazz. But then we
     766             :  * decided to add constants, and all of a sudden that means runtime type
     767             :  * validation and inference, except the "runtime" is the code generator
     768             :  * runtime.
     769             :  */
     770       37530 : void validate_const_rec(std::string name, t_type* type, t_const_value* value) {
     771       37530 :   if (type->is_void()) {
     772           0 :     throw "type error: cannot declare a void const: " + name;
     773             :   }
     774             : 
     775       37530 :   if (type->is_base_type()) {
     776       26119 :     t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
     777       26119 :     switch (tbase) {
     778       19924 :     case t_base_type::TYPE_STRING:
     779       19924 :       if (value->get_type() != t_const_value::CV_STRING) {
     780           0 :         throw "type error: const \"" + name + "\" was declared as string";
     781             :       }
     782       19924 :       break;
     783             : #ifdef SANDESH
     784           0 :     case t_base_type::TYPE_STATIC_CONST_STRING:
     785           0 :       if (value->get_type() != t_const_value::CV_STRING) {
     786           0 :         throw "type error: const \"" + name + "\" was declared as static const string";
     787             :       }
     788           0 :       break;
     789        1037 :     case t_base_type::TYPE_U16:
     790        1037 :       if (value->get_type() != t_const_value::CV_INTEGER) {
     791           0 :         throw "type error: const \"" + name + "\" was declared as u16";
     792             :       }
     793        1037 :       break;
     794          66 :     case t_base_type::TYPE_U32:
     795          66 :       if (value->get_type() != t_const_value::CV_INTEGER) {
     796           0 :         throw "type error: const \"" + name + "\" was declared as u32";
     797             :       }
     798          66 :       break;
     799          16 :     case t_base_type::TYPE_U64:
     800          16 :       if (value->get_type() != t_const_value::CV_INTEGER) {
     801           0 :         throw "type error: const \"" + name + "\" was declared as u64";
     802             :       }
     803          16 :       break;
     804          11 :     case t_base_type::TYPE_UUID:
     805          11 :       if (value->get_type() != t_const_value::CV_STRING) {
     806           0 :         throw "type error: const \"" + name + "\" was declared as ct_uuid_t";
     807             :       }
     808          11 :       break;
     809           0 :     case t_base_type::TYPE_IPADDR:
     810           0 :       if (value->get_type() != t_const_value::CV_STRING) {
     811           0 :         throw "type error: const \"" + name + "\" was declared as ipaddr";
     812             :       }
     813           0 :       break;
     814             : #endif
     815        4796 :     case t_base_type::TYPE_BOOL:
     816        4796 :       if (value->get_type() != t_const_value::CV_INTEGER) {
     817           0 :         throw "type error: const \"" + name + "\" was declared as bool";
     818             :       }
     819        4796 :       break;
     820         143 :     case t_base_type::TYPE_BYTE:
     821         143 :       if (value->get_type() != t_const_value::CV_INTEGER) {
     822           0 :         throw "type error: const \"" + name + "\" was declared as byte";
     823             :       }
     824         143 :       break;
     825          11 :     case t_base_type::TYPE_I16:
     826          11 :       if (value->get_type() != t_const_value::CV_INTEGER) {
     827           0 :         throw "type error: const \"" + name + "\" was declared as i16";
     828             :       }
     829          11 :       break;
     830         115 :     case t_base_type::TYPE_I32:
     831         115 :       if (value->get_type() != t_const_value::CV_INTEGER) {
     832           0 :         throw "type error: const \"" + name + "\" was declared as i32";
     833             :       }
     834         115 :       break;
     835           0 :     case t_base_type::TYPE_I64:
     836           0 :       if (value->get_type() != t_const_value::CV_INTEGER) {
     837           0 :         throw "type error: const \"" + name + "\" was declared as i64";
     838             :       }
     839           0 :       break;
     840           0 :     case t_base_type::TYPE_DOUBLE:
     841           0 :       if (value->get_type() != t_const_value::CV_INTEGER &&
     842           0 :           value->get_type() != t_const_value::CV_DOUBLE) {
     843           0 :         throw "type error: const \"" + name + "\" was declared as double";
     844             :       }
     845           0 :       break;
     846           0 :     default:
     847           0 :       throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase) + name;
     848             :     }
     849       11411 :   } else if (type->is_enum()) {
     850        4972 :     if (value->get_type() != t_const_value::CV_IDENTIFIER) {
     851           0 :       throw "type error: const \"" + name + "\" was declared as enum";
     852             :     }
     853             : 
     854             :     // see if there's a dot in the identifier
     855        4972 :     std::string name_portion = value->get_identifier_name();
     856             : 
     857        4972 :     const vector<t_enum_value*>& enum_values = ((t_enum*)type)->get_constants();
     858        4972 :     vector<t_enum_value*>::const_iterator c_iter;
     859        4972 :     bool found = false;
     860             : 
     861       42524 :     for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
     862       42524 :       if ((*c_iter)->get_name() == name_portion) {
     863        4972 :         found = true;
     864        4972 :         break;
     865             :       }
     866             :     }
     867        4972 :     if (!found) {
     868           0 :       throw "type error: const " + name + " was declared as type "
     869           0 :         + type->get_name() + " which is an enum, but "
     870           0 :         + value->get_identifier() + " is not a valid value for that enum";
     871             :     }
     872       11411 :   } else if (type->is_struct() || type->is_xception()) {
     873        4940 :     if (value->get_type() != t_const_value::CV_MAP) {
     874           0 :       throw "type error: const \"" + name + "\" was declared as struct/xception";
     875             :     }
     876        4940 :     const vector<t_field*>& fields = ((t_struct*)type)->get_members();
     877        4940 :     vector<t_field*>::const_iterator f_iter;
     878             : 
     879        4940 :     const map<t_const_value*, t_const_value*>& val = value->get_map();
     880        4940 :     map<t_const_value*, t_const_value*>::const_iterator v_iter;
     881       20800 :     for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
     882       15860 :       if (v_iter->first->get_type() != t_const_value::CV_STRING) {
     883           0 :         throw "type error: " + name + " struct key must be string";
     884             :       }
     885       15860 :       t_type* field_type = NULL;
     886      100556 :       for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
     887       84696 :         if ((*f_iter)->get_name() == v_iter->first->get_string()) {
     888       15860 :           field_type = (*f_iter)->get_type();
     889             :         }
     890             :       }
     891       15860 :       if (field_type == NULL) {
     892           0 :         throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
     893             :       }
     894             : 
     895       15860 :       validate_const_rec(name + "." + v_iter->first->get_string(), field_type, v_iter->second);
     896             :     }
     897        1499 :   } else if (type->is_map()) {
     898         535 :     t_type* k_type = ((t_map*)type)->get_key_type();
     899         535 :     t_type* v_type = ((t_map*)type)->get_val_type();
     900         535 :     const map<t_const_value*, t_const_value*>& val = value->get_map();
     901         535 :     map<t_const_value*, t_const_value*>::const_iterator v_iter;
     902        6207 :     for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
     903        5672 :       validate_const_rec(name + "<key>", k_type, v_iter->first);
     904        5672 :       validate_const_rec(name + "<val>", v_type, v_iter->second);
     905             :     }
     906         964 :   } else if (type->is_list() || type->is_set()) {
     907             :     t_type* e_type;
     908         964 :     if (type->is_list()) {
     909         964 :       e_type = ((t_list*)type)->get_elem_type();
     910             :     } else {
     911           0 :       e_type = ((t_set*)type)->get_elem_type();
     912             :     }
     913         964 :     const vector<t_const_value*>& val = value->get_list();
     914         964 :     vector<t_const_value*>::const_iterator v_iter;
     915        7641 :     for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
     916        6677 :       validate_const_rec(name + "<elem>", e_type, *v_iter);
     917             :     }
     918             :   }
     919       37530 : }
     920             : 
     921             : /**
     922             :  * Check the type of the parsed const information against its declared type
     923             :  */
     924        3481 : void validate_const_type(t_const* c) {
     925        3481 :   validate_const_rec(c->get_name(), c->get_type(), c->get_value());
     926        3481 : }
     927             : 
     928             : /**
     929             :  * Check the type of a default value assigned to a field.
     930             :  */
     931         168 : void validate_field_value(t_field* field, t_const_value* cv) {
     932         168 :   validate_const_rec(field->get_name(), field->get_type(), cv);
     933         168 : }
     934             : 
     935             : /**
     936             :  * Check that all the elements of a throws block are actually exceptions.
     937             :  */
     938           0 : bool validate_throws(t_struct* throws) {
     939           0 :   const vector<t_field*>& members = throws->get_members();
     940           0 :   vector<t_field*>::const_iterator m_iter;
     941           0 :   for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
     942           0 :     if (!t_generator::get_true_type((*m_iter)->get_type())->is_xception()) {
     943           0 :       return false;
     944             :     }
     945             :   }
     946           0 :   return true;
     947             : }
     948             : 
     949             : /**
     950             :  * Parses a program
     951             :  */
     952         776 : void parse(t_program* program, t_program* parent_program) {
     953             :   // Get scope file path
     954         776 :   string path = program->get_path();
     955             : 
     956             :   // Set current dir global, which is used in the include_file function
     957         776 :   g_curdir = directory_name(path);
     958         776 :   g_curpath = path;
     959             : 
     960             :   // Open the file
     961         776 :   yyin = fopen(path.c_str(), "r");
     962         776 :   if (yyin == 0) {
     963           0 :     failure("Could not open input file: \"%s\"", path.c_str());
     964             :   }
     965             : 
     966             :   // Create new scope and scan for includes
     967         776 :   pverbose("Scanning %s for includes\n", path.c_str());
     968         776 :   g_parse_mode = INCLUDES;
     969         776 :   g_program = program;
     970         776 :   g_scope = program->scope();
     971             :   try {
     972         776 :     yylineno = 1;
     973         776 :     if (yyparse() != 0) {
     974           0 :       failure("Parser error during include pass.");
     975             :     }
     976           0 :   } catch (string x) {
     977           0 :     failure(x.c_str());
     978           0 :   }
     979         776 :   fclose(yyin);
     980             : 
     981             :   // Recursively parse all the include programs
     982         776 :   vector<t_program*>& includes = program->get_includes();
     983         776 :   vector<t_program*>::iterator iter;
     984         990 :   for (iter = includes.begin(); iter != includes.end(); ++iter) {
     985         214 :     parse(*iter, program);
     986             :   }
     987             : 
     988             :   // reset program doctext status before parsing a new file
     989         776 :   reset_program_doctext_info();
     990             : 
     991             :   // Parse the program file
     992         776 :   g_parse_mode = PROGRAM;
     993         776 :   g_program = program;
     994         776 :   g_scope = program->scope();
     995         776 :   g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL;
     996         776 :   g_parent_prefix = program->get_name() + ".";
     997         776 :   g_curpath = path;
     998         776 :   yyin = fopen(path.c_str(), "r");
     999         776 :   if (yyin == 0) {
    1000           0 :     failure("Could not open input file: \"%s\"", path.c_str());
    1001             :   }
    1002         776 :   pverbose("Parsing %s for types\n", path.c_str());
    1003         776 :   yylineno = 1;
    1004             :   try {
    1005         776 :     if (yyparse() != 0) {
    1006           0 :       failure("Parser error during types pass.");
    1007             :     }
    1008           0 :   } catch (string x) {
    1009           0 :     failure(x.c_str());
    1010           0 :   }
    1011         776 :   fclose(yyin);
    1012         776 : }
    1013             : 
    1014             : /**
    1015             :  * Generate code
    1016             :  */
    1017         562 : void generate(t_program* program, const vector<string>& generator_strings) {
    1018             :   // Oooohh, recursive code generation, hot!!
    1019         562 :   if (gen_recurse) {
    1020           0 :     const vector<t_program*>& includes = program->get_includes();
    1021           0 :     for (size_t i = 0; i < includes.size(); ++i) {
    1022             :       // Propogate output path from parent to child programs
    1023           0 :       includes[i]->set_out_path(program->get_out_path(), program->is_out_path_absolute());
    1024             : 
    1025           0 :       generate(includes[i], generator_strings);
    1026             :     }
    1027             :   }
    1028             : 
    1029             :   // Generate code!
    1030             :   try {
    1031         562 :     pverbose("Program: %s\n", program->get_path().c_str());
    1032             : 
    1033             :     // Compute fingerprints.
    1034         562 :     generate_all_fingerprints(program);
    1035             : 
    1036         562 :     if (dump_docs) {
    1037           0 :       dump_docstrings(program);
    1038             :     }
    1039             : 
    1040         562 :     vector<string>::const_iterator iter;
    1041        1325 :     for (iter = generator_strings.begin(); iter != generator_strings.end(); ++iter) {
    1042         763 :       t_generator* generator = t_generator_registry::get_generator(program, *iter);
    1043             : 
    1044         763 :       if (generator == NULL) {
    1045           0 :         pwarning(1, "Unable to get a generator for \"%s\".\n", iter->c_str());
    1046             :       } else {
    1047         763 :         pverbose("Generating \"%s\"\n", iter->c_str());
    1048         763 :         generator->generate_program();
    1049         763 :         delete generator;
    1050             :       }
    1051             :     }
    1052             : 
    1053           0 :   } catch (string s) {
    1054           0 :     printf("Error: %s\n", s.c_str());
    1055             : #ifdef SANDESH
    1056           0 :     failure("Error: %s\n", s.c_str());
    1057             : #endif
    1058           0 :   } catch (const char* exc) {
    1059           0 :     printf("Error: %s\n", exc);
    1060             : #ifdef SANDESH
    1061           0 :     failure("Error: %s\n", exc);
    1062             : #endif
    1063           0 :   }
    1064         562 : }
    1065             : 
    1066             : /**
    1067             :  * Parse it up.. then spit it back out, in pretty much every language. Alright
    1068             :  * not that many languages, but the cool ones that we care about.
    1069             :  */
    1070         959 : int main(int argc, char** argv) {
    1071             :   int i;
    1072         959 :   std::string out_path;
    1073         959 :   bool out_path_is_absolute = false;
    1074             : 
    1075             :   // Setup time string
    1076         959 :   time_t now = time(NULL);
    1077         959 :   g_time_str = ctime(&now);
    1078             : 
    1079             :   // Check for necessary arguments, you gotta have at least a filename and
    1080             :   // an output language flag
    1081         959 :   if (argc < 2) {
    1082           0 :     usage();
    1083             :   }
    1084             : 
    1085         959 :   vector<string> generator_strings;
    1086             : 
    1087             :   // Set the current path to a dummy value to make warning messages clearer.
    1088         959 :   g_curpath = "arguments";
    1089             : 
    1090             :   // Hacky parameter handling... I didn't feel like using a library sorry!
    1091        3398 :   for (i = 1; i < argc-1; i++) {
    1092             :     char* arg;
    1093             : 
    1094             :     char *saveptr;
    1095        2439 :     arg = strtok_r(argv[i], " ", &saveptr);
    1096        4878 :     while (arg != NULL) {
    1097             :       // Treat double dashes as single dashes
    1098        2439 :       if (arg[0] == '-' && arg[1] == '-') {
    1099         764 :         ++arg;
    1100             :       }
    1101             : 
    1102        2439 :       if (strcmp(arg, "-version") == 0) {
    1103           0 :         version();
    1104           0 :         exit(1);
    1105        2439 :       } else if (strcmp(arg, "-debug") == 0) {
    1106           0 :         g_debug = 1;
    1107        2439 :       } else if (strcmp(arg, "-nowarn") == 0) {
    1108           0 :         g_warn = 0;
    1109        2439 :       } else if (strcmp(arg, "-strict") == 0) {
    1110           0 :         g_strict = 255;
    1111           0 :         g_warn = 2;
    1112        2439 :       } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0 ) {
    1113           0 :         g_verbose = 1;
    1114        2439 :       } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0 ) {
    1115           0 :         gen_recurse = true;
    1116        2439 :       } else if (strcmp(arg, "-allow-neg-keys") == 0) {
    1117           0 :         g_allow_neg_field_keys = true;
    1118        2439 :       } else if (strcmp(arg, "-allow-64bit-consts") == 0) {
    1119           0 :         g_allow_64bit_consts = true;
    1120        2439 :       } else if (strcmp(arg, "-gen") == 0) {
    1121         763 :         arg = argv[++i];
    1122         763 :         if (arg == NULL) {
    1123           0 :           fprintf(stderr, "!!! Missing generator specification\n");
    1124           0 :           usage();
    1125             :         }
    1126         763 :         generator_strings.push_back(arg);
    1127        1676 :       } else if (strcmp(arg, "-dense") == 0) {
    1128           0 :         gen_dense = true;
    1129        1676 :       } else if (strcmp(arg, "-cpp") == 0) {
    1130           0 :         gen_cpp = true;
    1131        1676 :       } else if (strcmp(arg, "-javabean") == 0) {
    1132           0 :         gen_javabean = true;
    1133        1676 :       } else if (strcmp(arg, "-java") == 0) {
    1134           0 :         gen_java = true;
    1135        1676 :       } else if (strcmp(arg, "-php") == 0) {
    1136           0 :         gen_php = true;
    1137        1676 :       } else if (strcmp(arg, "-phpi") == 0) {
    1138           0 :         gen_phpi = true;
    1139        1676 :       } else if (strcmp(arg, "-phps") == 0) {
    1140           0 :         gen_php = true;
    1141           0 :         gen_phps = true;
    1142        1676 :       } else if (strcmp(arg, "-phpl") == 0) {
    1143           0 :         gen_php = true;
    1144           0 :         gen_phps = false;
    1145        1676 :       } else if (strcmp(arg, "-phpa") == 0) {
    1146           0 :         gen_php = true;
    1147           0 :         gen_phps = false;
    1148           0 :         gen_phpa = true;
    1149        1676 :       } else if (strcmp(arg, "-phpo") == 0) {
    1150           0 :         gen_php = true;
    1151           0 :         gen_phpo = true;
    1152        1676 :       } else if (strcmp(arg, "-rest") == 0) {
    1153           0 :         gen_rest = true;
    1154        1676 :       } else if (strcmp(arg, "-py") == 0) {
    1155           0 :         gen_py = true;
    1156        1676 :       } else if (strcmp(arg, "-pyns") == 0) {
    1157           0 :         gen_py = true;
    1158           0 :         gen_py_newstyle = true;
    1159        1676 :       } else if (strcmp(arg, "-rb") == 0) {
    1160           0 :         gen_rb = true;
    1161        1676 :       } else if (strcmp(arg, "-xsd") == 0) {
    1162           0 :         gen_xsd = true;
    1163        1676 :       } else if (strcmp(arg, "-perl") == 0) {
    1164           0 :         gen_perl = true;
    1165        1676 :       } else if (strcmp(arg, "-erl") == 0) {
    1166           0 :         gen_erl = true;
    1167        1676 :       } else if (strcmp(arg, "-ocaml") == 0) {
    1168           0 :         gen_ocaml = true;
    1169        1676 :       } else if (strcmp(arg, "-hs") == 0) {
    1170           0 :         gen_hs = true;
    1171        1676 :       } else if (strcmp(arg, "-cocoa") == 0) {
    1172           0 :         gen_cocoa = true;
    1173        1676 :       } else if (strcmp(arg, "-st") == 0) {
    1174           0 :         gen_st = true;
    1175        1676 :       } else if (strcmp(arg, "-csharp") == 0) {
    1176           0 :         gen_csharp = true;
    1177        1676 :       } else if (strcmp(arg, "-delphi") == 0) {
    1178           0 :         gen_delphi = true;
    1179        1676 :       } else if (strcmp(arg, "-cpp_use_include_prefix") == 0) {
    1180           0 :         g_cpp_use_include_prefix = true;
    1181        1676 :       } else if (strcmp(arg, "-I") == 0) {
    1182             :         // An argument of "-I\ asdf" is invalid and has unknown results
    1183        1114 :         arg = argv[++i];
    1184             : 
    1185        1114 :         if (arg == NULL) {
    1186           0 :           fprintf(stderr, "!!! Missing Include directory\n");
    1187           0 :           usage();
    1188             :         }
    1189        1114 :         g_incl_searchpath.push_back(arg);
    1190         562 :       } else if ((strcmp(arg, "-o") == 0) || (strcmp(arg, "-out") == 0)) {
    1191         562 :         out_path_is_absolute = (strcmp(arg, "-out") == 0) ? true : false;
    1192             : 
    1193         562 :         arg = argv[++i];
    1194         562 :         if (arg == NULL) {
    1195           0 :           fprintf(stderr, "-o: missing output directory\n");
    1196           0 :           usage();
    1197             :         }
    1198         562 :         out_path = arg;
    1199             : 
    1200             :         struct stat sb;
    1201         562 :         if (stat(out_path.c_str(), &sb) < 0) {
    1202           0 :           fprintf(stderr, "Output directory %s is unusable: %s\n", out_path.c_str(), strerror(errno));
    1203           0 :           return -1;
    1204             :         }
    1205         562 :         if (! S_ISDIR(sb.st_mode)) {
    1206           0 :           fprintf(stderr, "Output directory %s exists but is not a directory\n", out_path.c_str());
    1207           0 :           return -1;
    1208             :         }
    1209         562 :       } else {
    1210           0 :         fprintf(stderr, "!!! Unrecognized option: %s\n", arg);
    1211           0 :         usage();
    1212             :       }
    1213             : 
    1214             :       // Tokenize more
    1215        2439 :       arg = strtok_r(NULL, " ", &saveptr);
    1216             :     }
    1217             :   }
    1218             : 
    1219             :   // if you're asking for version, you have a right not to pass a file
    1220         959 :   if (strcmp(argv[argc-1], "-version") == 0) {
    1221         397 :     version();
    1222         397 :     exit(1);
    1223             :   }
    1224             : 
    1225             :   // TODO(dreiss): Delete these when everyone is using the new hotness.
    1226         562 :   if (gen_cpp) {
    1227           0 :     pwarning(1, "-cpp is deprecated.  Use --gen cpp");
    1228           0 :     string gen_string = "cpp:";
    1229           0 :     if (gen_dense) {
    1230           0 :       gen_string.append("dense,");
    1231             :     }
    1232           0 :     if (g_cpp_use_include_prefix) {
    1233           0 :       gen_string.append("include_prefix,");
    1234             :     }
    1235           0 :     generator_strings.push_back(gen_string);
    1236           0 :   }
    1237         562 :   if (gen_java) {
    1238           0 :     pwarning(1, "-java is deprecated.  Use --gen java");
    1239           0 :     generator_strings.push_back("java");
    1240             :   }
    1241         562 :   if (gen_javabean) {
    1242           0 :     pwarning(1, "-javabean is deprecated.  Use --gen java:beans");
    1243           0 :     generator_strings.push_back("java:beans");
    1244             :   }
    1245         562 :   if (gen_csharp) {
    1246           0 :     pwarning(1, "-csharp is deprecated.  Use --gen csharp");
    1247           0 :     generator_strings.push_back("csharp");
    1248             :   }
    1249         562 :   if (gen_delphi) {
    1250           0 :     pwarning(1, "-delphi is deprecated.  Use --gen delphi");
    1251           0 :     generator_strings.push_back("delphi");
    1252             :   }
    1253         562 :   if (gen_py) {
    1254           0 :     pwarning(1, "-py is deprecated.  Use --gen py");
    1255           0 :     generator_strings.push_back("py");
    1256             :   }
    1257         562 :   if (gen_rb) {
    1258           0 :     pwarning(1, "-rb is deprecated.  Use --gen rb");
    1259           0 :     generator_strings.push_back("rb");
    1260             :   }
    1261         562 :   if (gen_perl) {
    1262           0 :     pwarning(1, "-perl is deprecated.  Use --gen perl");
    1263           0 :     generator_strings.push_back("perl");
    1264             :   }
    1265         562 :   if (gen_php || gen_phpi) {
    1266           0 :     pwarning(1, "-php is deprecated.  Use --gen php");
    1267           0 :     string gen_string = "php:";
    1268           0 :     if (gen_phpi) {
    1269           0 :       gen_string.append("inlined,");
    1270           0 :     } else if(gen_phps) {
    1271           0 :       gen_string.append("server,");
    1272           0 :     } else if(gen_phpa) {
    1273           0 :       gen_string.append("autoload,");
    1274           0 :     } else if(gen_phpo) {
    1275           0 :       gen_string.append("oop,");
    1276           0 :     } else if(gen_rest) {
    1277           0 :       gen_string.append("rest,");
    1278             :     }
    1279           0 :     generator_strings.push_back(gen_string);
    1280           0 :   }
    1281         562 :   if (gen_cocoa) {
    1282           0 :     pwarning(1, "-cocoa is deprecated.  Use --gen cocoa");
    1283           0 :     generator_strings.push_back("cocoa");
    1284             :   }
    1285         562 :   if (gen_erl) {
    1286           0 :     pwarning(1, "-erl is deprecated.  Use --gen erl");
    1287           0 :     generator_strings.push_back("erl");
    1288             :   }
    1289         562 :   if (gen_st) {
    1290           0 :     pwarning(1, "-st is deprecated.  Use --gen st");
    1291           0 :     generator_strings.push_back("st");
    1292             :   }
    1293         562 :   if (gen_ocaml) {
    1294           0 :     pwarning(1, "-ocaml is deprecated.  Use --gen ocaml");
    1295           0 :     generator_strings.push_back("ocaml");
    1296             :   }
    1297         562 :   if (gen_hs) {
    1298           0 :     pwarning(1, "-hs is deprecated.  Use --gen hs");
    1299           0 :     generator_strings.push_back("hs");
    1300             :   }
    1301         562 :   if (gen_xsd) {
    1302           0 :     pwarning(1, "-xsd is deprecated.  Use --gen xsd");
    1303           0 :     generator_strings.push_back("xsd");
    1304             :   }
    1305             : 
    1306             :   // You gotta generate something!
    1307         562 :   if (generator_strings.empty()) {
    1308           0 :     fprintf(stderr, "!!! No output language(s) specified\n\n");
    1309           0 :     usage();
    1310             :   }
    1311             : 
    1312             :   // Real-pathify it
    1313             :   char rp[PATH_MAX];
    1314         562 :   if (argv[i] == NULL) {
    1315           0 :     fprintf(stderr, "!!! Missing file name\n");
    1316           0 :     usage();
    1317             :   }
    1318         562 :   if (saferealpath(argv[i], rp) == NULL) {
    1319           0 :     failure("Could not open input file with realpath: %s", argv[i]);
    1320             :   }
    1321         562 :   string input_file(rp);
    1322             : 
    1323             :   // Instance of the global parse tree
    1324         562 :   t_program* program = new t_program(input_file);
    1325         562 :   if (out_path.size()) {
    1326         562 :     program->set_out_path(out_path, out_path_is_absolute);
    1327             :   }
    1328             : 
    1329             :   // Compute the cpp include prefix.
    1330             :   // infer this from the filename passed in
    1331         562 :   string input_filename = argv[i];
    1332         562 :   string include_prefix;
    1333             : 
    1334         562 :   string::size_type last_slash = string::npos;
    1335         562 :   if ((last_slash = input_filename.rfind("/")) != string::npos) {
    1336         562 :     include_prefix = input_filename.substr(0, last_slash);
    1337             :   }
    1338             : 
    1339         562 :   program->set_include_prefix(include_prefix);
    1340             : 
    1341             :   // Initialize global types
    1342         562 :   g_type_void   = new t_base_type("void",   t_base_type::TYPE_VOID);
    1343         562 :   g_type_string = new t_base_type("string", t_base_type::TYPE_STRING);
    1344         562 :   g_type_binary = new t_base_type("string", t_base_type::TYPE_STRING);
    1345         562 :   ((t_base_type*)g_type_binary)->set_binary(true);
    1346         562 :   g_type_slist  = new t_base_type("string", t_base_type::TYPE_STRING);
    1347         562 :   ((t_base_type*)g_type_slist)->set_string_list(true);
    1348         562 :   g_type_bool   = new t_base_type("bool",   t_base_type::TYPE_BOOL);
    1349         562 :   g_type_byte   = new t_base_type("byte",   t_base_type::TYPE_BYTE);
    1350         562 :   g_type_i16    = new t_base_type("i16",    t_base_type::TYPE_I16);
    1351         562 :   g_type_i32    = new t_base_type("i32",    t_base_type::TYPE_I32);
    1352         562 :   g_type_i64    = new t_base_type("i64",    t_base_type::TYPE_I64);
    1353         562 :   g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
    1354             : #ifdef SANDESH
    1355         562 :   g_type_u16    = new t_base_type("u16", t_base_type::TYPE_U16);
    1356         562 :   g_type_u32    = new t_base_type("u32", t_base_type::TYPE_U32);
    1357         562 :   g_type_u64    = new t_base_type("u64", t_base_type::TYPE_U64);
    1358         562 :   g_type_ipv4    = new t_base_type("ipv4", t_base_type::TYPE_IPV4);
    1359         562 :   g_type_ipaddr  = new t_base_type("ipaddr", t_base_type::TYPE_IPADDR);
    1360         562 :   g_type_xml    = new t_base_type("xml", t_base_type::TYPE_XML);
    1361         562 :   g_type_uuid_t    = new t_base_type("ct_uuid_t", t_base_type::TYPE_UUID);
    1362         562 :   g_type_static_const_string = new t_base_type("static const string", t_base_type::TYPE_STATIC_CONST_STRING);
    1363         562 :   g_type_sandesh_system   = new t_base_type("system",   t_base_type::TYPE_SANDESH_SYSTEM);
    1364         562 :   g_type_sandesh_request  = new t_base_type("request",  t_base_type::TYPE_SANDESH_REQUEST);
    1365         562 :   g_type_sandesh_response = new t_base_type("response", t_base_type::TYPE_SANDESH_RESPONSE);
    1366         562 :   g_type_sandesh_trace    = new t_base_type("trace",    t_base_type::TYPE_SANDESH_TRACE);
    1367         562 :   g_type_sandesh_trace_object = new t_base_type("traceobject", t_base_type::TYPE_SANDESH_TRACE_OBJECT);
    1368         562 :   g_type_sandesh_buffer   = new t_base_type("buffer",   t_base_type::TYPE_SANDESH_BUFFER);
    1369         562 :   g_type_sandesh_uve      = new t_base_type("uve",      t_base_type::TYPE_SANDESH_UVE);
    1370         562 :   g_type_sandesh_dynamic_uve = new t_base_type("dynamicuve", t_base_type::TYPE_SANDESH_DYNAMIC_UVE);
    1371         562 :   g_type_sandesh_alarm    = new t_base_type("alarm",    t_base_type::TYPE_SANDESH_ALARM);
    1372         562 :   g_type_sandesh_object   = new t_base_type("object",   t_base_type::TYPE_SANDESH_OBJECT);
    1373         562 :   g_type_sandesh_flow     = new t_base_type("flow",     t_base_type::TYPE_SANDESH_FLOW);
    1374         562 :   g_type_sandesh_session = new t_base_type("session", t_base_type::TYPE_SANDESH_SESSION);
    1375             : #endif
    1376             : 
    1377             :   // Parse it!
    1378         562 :   parse(program, NULL);
    1379             : 
    1380             :   // The current path is not really relevant when we are doing generation.
    1381             :   // Reset the variable to make warning messages clearer.
    1382         562 :   g_curpath = "generation";
    1383             :   // Reset yylineno for the heck of it.  Use 1 instead of 0 because
    1384             :   // That is what shows up during argument parsing.
    1385         562 :   yylineno = 1;
    1386             :   // Generate it!
    1387         562 :   generate(program, generator_strings);
    1388             : 
    1389             :   // Clean up. Who am I kidding... this program probably orphans heap memory
    1390             :   // all over the place, but who cares because it is about to exit and it is
    1391             :   // all referenced and used by this wacky parse tree up until now anyways.
    1392             : 
    1393         562 :   delete program;
    1394         562 :   delete g_type_void;
    1395         562 :   delete g_type_string;
    1396         562 :   delete g_type_bool;
    1397         562 :   delete g_type_byte;
    1398         562 :   delete g_type_i16;
    1399         562 :   delete g_type_i32;
    1400         562 :   delete g_type_i64;
    1401         562 :   delete g_type_double;
    1402             : #ifdef SANDESH
    1403         562 :   delete g_type_u16;
    1404         562 :   delete g_type_u32;
    1405         562 :   delete g_type_u64;
    1406         562 :   delete g_type_ipv4;
    1407         562 :   delete g_type_ipaddr;
    1408         562 :   delete g_type_static_const_string;
    1409         562 :   delete g_type_sandesh_system;
    1410         562 :   delete g_type_sandesh_request;
    1411         562 :   delete g_type_sandesh_response;
    1412         562 :   delete g_type_sandesh_trace;
    1413         562 :   delete g_type_sandesh_trace_object;
    1414         562 :   delete g_type_sandesh_buffer;
    1415         562 :   delete g_type_sandesh_uve;
    1416         562 :   delete g_type_sandesh_dynamic_uve;
    1417         562 :   delete g_type_sandesh_alarm;
    1418         562 :   delete g_type_sandesh_object;
    1419         562 :   delete g_type_sandesh_flow;
    1420         562 :   delete g_type_sandesh_session;
    1421         562 :   delete g_type_xml;
    1422         562 :   delete g_type_uuid_t;
    1423             : 
    1424             : #endif
    1425             : 
    1426             :   // Finished
    1427         562 :   return 0;
    1428         562 : }

Generated by: LCOV version 1.14