OpenSDN source code
src/contrail-common/sandesh/compiler/main.cc
Go to the documentation of this file.
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 
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 
66 
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 
110 
115 
120 
125 
129 string g_curdir;
130 
134 string g_curpath;
135 
139 vector<string> g_incl_searchpath;
140 
146 
150 int g_debug = 0;
151 
155 int g_strict = 127;
156 
160 int g_warn = 1;
161 
165 int g_verbose = 0;
166 
171 
175 char* g_doctext;
176 
181 
188 
193 
198 
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 
230 char *saferealpath(const char *path, char *resolved_path) {
231  return realpath(path, resolved_path);
232 }
233 
234 
243 void yyerror(const char* fmt, ...) {
244  va_list args;
245  fprintf(stderr,
246  "[ERROR:%s:%d] (last token was '%s')\n",
247  g_curpath.c_str(),
248  yylineno,
249  yytext);
250 
251  va_start(args, fmt);
252  vfprintf(stderr, fmt, args);
253  va_end(args);
254 
255  fprintf(stderr, "\n");
256 }
257 
263 void pdebug(const char* fmt, ...) {
264  if (g_debug == 0) {
265  return;
266  }
267  va_list args;
268  printf("[PARSE:%d] ", yylineno);
269  va_start(args, fmt);
270  vprintf(fmt, args);
271  va_end(args);
272  printf("\n");
273 }
274 
280 void pverbose(const char* fmt, ...) {
281  if (g_verbose == 0) {
282  return;
283  }
284  va_list args;
285  va_start(args, fmt);
286  vprintf(fmt, args);
287  va_end(args);
288 }
289 
295 void pwarning(int level, const char* fmt, ...) {
296  if (g_warn < level) {
297  return;
298  }
299  va_list args;
300  printf("[WARNING:%s:%d] ", g_curpath.c_str(), yylineno);
301  va_start(args, fmt);
302  vprintf(fmt, args);
303  va_end(args);
304  printf("\n");
305 }
306 
312 void failure(const char* fmt, ...) {
313  va_list args;
314  fprintf(stderr, "[FAILURE:%s:%d] ", g_curpath.c_str(), yylineno);
315  va_start(args, fmt);
316  vfprintf(stderr, fmt, args);
317  va_end(args);
318  printf("\n");
319  exit(1);
320 }
321 
325 string program_name(string filename) {
326  string::size_type slash = filename.rfind("/");
327  if (slash != string::npos) {
328  filename = filename.substr(slash+1);
329  }
330  string::size_type dot = filename.rfind(".");
331  if (dot != string::npos) {
332  filename = filename.substr(0, dot);
333  }
334  return filename;
335 }
336 
340 string directory_name(string filename) {
341  string::size_type slash = filename.rfind("/");
342  // No slash, just use the current directory
343  if (slash == string::npos) {
344  return ".";
345  }
346  return filename.substr(0, slash);
347 }
348 
352 string include_file(string filename) {
353  // Absolute path? Just try that
354  if (filename[0] == '/') {
355  // Realpath!
356  char rp[PATH_MAX];
357  if (saferealpath(filename.c_str(), rp) == NULL) {
358  pwarning(0, "Cannot open include file %s\n", filename.c_str());
359  return std::string();
360  }
361 
362  // Stat this file
363  struct stat finfo;
364  if (stat(rp, &finfo) == 0) {
365  return rp;
366  }
367  } else { // relative path, start searching
368  // new search path with current dir global
369  vector<string> sp = g_incl_searchpath;
370  sp.insert(sp.begin(), g_curdir);
371 
372  // iterate through paths
373  vector<string>::iterator it;
374  for (it = sp.begin(); it != sp.end(); it++) {
375  string sfilename = *(it) + "/" + filename;
376 
377  // Realpath!
378  char rp[PATH_MAX];
379  if (saferealpath(sfilename.c_str(), rp) == NULL) {
380  continue;
381  }
382 
383  // Stat this files
384  struct stat finfo;
385  if (stat(rp, &finfo) == 0) {
386  return rp;
387  }
388  }
389  }
390 
391  // Uh oh
392  pwarning(0, "Could not find include file %s\n", filename.c_str());
393  return std::string();
394 }
395 
401  if (g_doctext != NULL) {
402  pwarning(2, "Uncaptured doctext at on line %d.", g_doctext_lineno);
403  }
404  free(g_doctext);
405  g_doctext = NULL;
406 }
407 
412  if(g_program_doctext_candidate != NULL) {
415  }
418  pdebug("%s","program doctext set to INVALID");
419 }
420 
427  pdebug("%s","program doctext set to ABSOLUTELY_SURE");
428  } else {
430  pdebug("%s","program doctext set to NO_PROGRAM_DOCTEXT");
431  }
432 }
433 
440 char* clean_up_doctext(char* doctext) {
441  // Convert to C++ string, and remove carriage returns if added.
442  string docstring = doctext;
443  docstring.erase(
444  remove(docstring.begin(), docstring.end(), '\r'),
445  docstring.end());
446 
447  // Separate into lines.
448  vector<string> lines;
449  string::size_type pos = string::npos;
450  string::size_type last;
451  while (true) {
452  last = (pos == string::npos) ? 0 : pos+1;
453  pos = docstring.find('\n', last);
454  if (pos == string::npos) {
455  // First bit of cleaning. If the last line is only whitespace, drop it.
456  string::size_type nonwhite = docstring.find_first_not_of(" \t", last);
457  if (nonwhite != string::npos) {
458  lines.push_back(docstring.substr(last));
459  }
460  break;
461  }
462  lines.push_back(docstring.substr(last, pos-last));
463  }
464 
465  // A very profound docstring.
466  if (lines.empty()) {
467  return NULL;
468  }
469 
470  // Clear leading whitespace from the first line.
471  pos = lines.front().find_first_not_of(" \t");
472  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  bool have_prefix = true;
477  bool found_prefix = false;
478  string::size_type prefix_len = 0;
479  vector<string>::iterator l_iter;
480  for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
481  if (l_iter->empty()) {
482  continue;
483  }
484 
485  pos = l_iter->find_first_not_of(" \t");
486  if (!found_prefix) {
487  if (pos != string::npos) {
488  if (l_iter->at(pos) == '*') {
489  found_prefix = true;
490  prefix_len = pos;
491  } else {
492  have_prefix = false;
493  break;
494  }
495  } else {
496  // Whitespace-only line. Truncate it.
497  l_iter->clear();
498  }
499  } else if (l_iter->size() > pos
500  && l_iter->at(pos) == '*'
501  && pos == prefix_len) {
502  // Business as usual.
503  } else if (pos == string::npos) {
504  // Whitespace-only line. Let's truncate it for them.
505  l_iter->clear();
506  } else {
507  // The pattern has been broken.
508  have_prefix = false;
509  break;
510  }
511  }
512 
513  // If our prefix survived, delete it from every line.
514  if (have_prefix) {
515  // Get the star too.
516  prefix_len++;
517  for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
518  l_iter->erase(0, prefix_len);
519  }
520  }
521 
522  // Now delete the minimum amount of leading whitespace from each line.
523  prefix_len = string::npos;
524  for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
525  if (l_iter->empty()) {
526  continue;
527  }
528  pos = l_iter->find_first_not_of(" \t");
529  if (pos != string::npos
530  && (prefix_len == string::npos || pos < prefix_len)) {
531  prefix_len = pos;
532  }
533  }
534 
535  // If our prefix survived, delete it from every line.
536  if (prefix_len != string::npos) {
537  for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
538  l_iter->erase(0, prefix_len);
539  }
540  }
541 
542  // Remove trailing whitespace from every line.
543  for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
544  pos = l_iter->find_last_not_of(" \t");
545  if (pos != string::npos && pos != l_iter->length()-1) {
546  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  if (lines.front().empty()) {
553  lines.erase(lines.begin());
554  }
555 
556  // Now rejoin the lines and copy them back into doctext.
557  docstring.clear();
558  for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
559  docstring += *l_iter;
560  docstring += '\n';
561  }
562 
563  assert(docstring.length() <= strlen(doctext));
564  strcpy(doctext, docstring.c_str());
565  return doctext;
566 }
567 
569 static bool dump_docs = false;
570 
576 void dump_docstrings(t_program* program) {
577 
578  string progdoc = program->get_doc();
579  if (!progdoc.empty()) {
580  printf("Whole program doc:\n%s\n", progdoc.c_str());
581  }
582  const vector<t_typedef*>& typedefs = program->get_typedefs();
583  vector<t_typedef*>::const_iterator t_iter;
584  for (t_iter = typedefs.begin(); t_iter != typedefs.end(); ++t_iter) {
585  t_typedef* td = *t_iter;
586  if (td->has_doc()) {
587  printf("typedef %s:\n%s\n", td->get_name().c_str(), td->get_doc().c_str());
588  }
589  }
590  const vector<t_enum*>& enums = program->get_enums();
591  vector<t_enum*>::const_iterator e_iter;
592  for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
593  t_enum* en = *e_iter;
594  if (en->has_doc()) {
595  printf("enum %s:\n%s\n", en->get_name().c_str(), en->get_doc().c_str());
596  }
597  }
598  const vector<t_const*>& consts = program->get_consts();
599  vector<t_const*>::const_iterator c_iter;
600  for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
601  t_const* co = *c_iter;
602  if (co->has_doc()) {
603  printf("const %s:\n%s\n", co->get_name().c_str(), co->get_doc().c_str());
604  }
605  }
606  const vector<t_struct*>& structs = program->get_structs();
607  vector<t_struct*>::const_iterator s_iter;
608  for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
609  t_struct* st = *s_iter;
610  if (st->has_doc()) {
611  printf("struct %s:\n%s\n", st->get_name().c_str(), st->get_doc().c_str());
612  }
613  const vector<t_field*>& fields = st->get_members();
614  vector<t_field*>::const_iterator f_iter;
615  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
616  t_field* fp = *f_iter;
617  if (fp->has_doc()) {
618  printf("fields %s:\n%s\n",
619  fp->get_name().c_str(), fp->get_doc().c_str());
620  }
621  }
622  }
623  const vector<t_struct*>& xceptions = program->get_xceptions();
624  vector<t_struct*>::const_iterator x_iter;
625  for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
626  t_struct* xn = *x_iter;
627  if (xn->has_doc()) {
628  printf("xception %s:\n%s\n", xn->get_name().c_str(), xn->get_doc().c_str());
629  }
630  }
631 #ifdef SANDESH
632  const vector<t_sandesh*>& sandeshs = program->get_sandeshs();
633  vector<t_sandesh*>::const_iterator sn_iter;
634  for (sn_iter = sandeshs.begin(); sn_iter != sandeshs.end(); ++sn_iter) {
635  t_sandesh* sn = *sn_iter;
636  if (sn->has_doc()) {
637  printf("sandesh %s:\n%s\n", sn->get_name().c_str(), sn->get_doc().c_str());
638  }
639  const vector<t_field*>& sn_fields = sn->get_members();
640  vector<t_field*>::const_iterator snf_iter;
641  for (snf_iter = sn_fields.begin(); snf_iter != sn_fields.end(); ++snf_iter) {
642  t_field* snfp = *snf_iter;
643  if (snfp->has_doc()) {
644  printf("fields %s:\n%s\n",
645  snfp->get_name().c_str(), snfp->get_doc().c_str());
646  }
647  }
648  }
649 #endif
650  const vector<t_service*>& services = program->get_services();
651  vector<t_service*>::const_iterator v_iter;
652  for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
653  t_service* sv = *v_iter;
654  if (sv->has_doc()) {
655  printf("service %s:\n%s\n", sv->get_name().c_str(), sv->get_doc().c_str());
656  }
657  }
658 }
659 
664  const vector<t_struct*>& structs = program->get_structs();
665  vector<t_struct*>::const_iterator s_iter;
666  for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
667  t_struct* st = *s_iter;
668  st->generate_fingerprint();
669  }
670 
671  const vector<t_struct*>& xceptions = program->get_xceptions();
672  vector<t_struct*>::const_iterator x_iter;
673  for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
674  t_struct* st = *x_iter;
675  st->generate_fingerprint();
676  }
677 
678  const vector<t_enum*>& enums = program->get_enums();
679  vector<t_enum*>::const_iterator e_iter;
680  for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
681  t_enum* e = *e_iter;
683  }
684 
686 
687 #ifdef SANDESH
688  {
689  const vector<t_sandesh*>& sandeshs = program->get_sandeshs();
690  vector<t_sandesh*>::const_iterator s_iter;
691  for (s_iter = sandeshs.begin(); s_iter != sandeshs.end(); ++s_iter) {
692  t_sandesh* st = *s_iter;
693  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 }
707 
711 void version() {
712 #ifdef SANDESH
713  printf("Sandesh version %s\n", SANDESH_VERSION);
714 #else
715  printf("Thrift version %s\n", THRIFT_VERSION);
716 #endif
717 }
718 
722 void usage() {
723 #ifdef SANDESH
724  fprintf(stderr, "Usage: sandesh [options] file\n");
725 #else
726  fprintf(stderr, "Usage: thrift [options] file\n");
727 #endif
728  fprintf(stderr, "Options:\n");
729  fprintf(stderr, " -version Print the compiler version\n");
730  fprintf(stderr, " -o dir Set the output directory for gen-* packages\n");
731  fprintf(stderr, " (default: current directory)\n");
732  fprintf(stderr, " -out dir Set the ouput location for generated files.\n");
733  fprintf(stderr," (no gen-* folder will be created)\n");
734  fprintf(stderr, " -I dir Add a directory to the list of directories\n");
735  fprintf(stderr, " searched for include directives\n");
736  fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n");
737  fprintf(stderr, " -strict Strict compiler warnings on\n");
738  fprintf(stderr, " -v[erbose] Verbose mode\n");
739  fprintf(stderr, " -r[ecurse] Also generate included files\n");
740  fprintf(stderr, " -debug Parse debug trace to stdout\n");
741  fprintf(stderr, " --allow-neg-keys Allow negative field keys (Used to "
742  "preserve protocol\n");
743  fprintf(stderr, " compatibility with older .thrift files)\n");
744  fprintf(stderr, " --allow-64bit-consts Do not print warnings about using 64-bit constants\n");
745  fprintf(stderr, " --gen STR Generate code with a dynamically-registered generator.\n");
746  fprintf(stderr, " STR has the form language[:key1=val1[,key2,[key3=val3]]].\n");
747  fprintf(stderr, " Keys and values are options passed to the generator.\n");
748  fprintf(stderr, " Many options will not require values.\n");
749  fprintf(stderr, "\n");
750  fprintf(stderr, "Available generators (and options):\n");
752  t_generator_registry::gen_map_t::iterator iter;
753  for (iter = gen_map.begin(); iter != gen_map.end(); ++iter) {
754  fprintf(stderr, " %s (%s):\n",
755  iter->second->get_short_name().c_str(),
756  iter->second->get_long_name().c_str());
757  fprintf(stderr, "%s", iter->second->get_documentation().c_str());
758  }
759  exit(1);
760 }
761 
770 void validate_const_rec(std::string name, t_type* type, t_const_value* value) {
771  if (type->is_void()) {
772  throw "type error: cannot declare a void const: " + name;
773  }
774 
775  if (type->is_base_type()) {
776  t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
777  switch (tbase) {
779  if (value->get_type() != t_const_value::CV_STRING) {
780  throw "type error: const \"" + name + "\" was declared as string";
781  }
782  break;
783 #ifdef SANDESH
784  case t_base_type::TYPE_STATIC_CONST_STRING:
785  if (value->get_type() != t_const_value::CV_STRING) {
786  throw "type error: const \"" + name + "\" was declared as static const string";
787  }
788  break;
789  case t_base_type::TYPE_U16:
790  if (value->get_type() != t_const_value::CV_INTEGER) {
791  throw "type error: const \"" + name + "\" was declared as u16";
792  }
793  break;
794  case t_base_type::TYPE_U32:
795  if (value->get_type() != t_const_value::CV_INTEGER) {
796  throw "type error: const \"" + name + "\" was declared as u32";
797  }
798  break;
799  case t_base_type::TYPE_U64:
800  if (value->get_type() != t_const_value::CV_INTEGER) {
801  throw "type error: const \"" + name + "\" was declared as u64";
802  }
803  break;
804  case t_base_type::TYPE_UUID:
805  if (value->get_type() != t_const_value::CV_STRING) {
806  throw "type error: const \"" + name + "\" was declared as ct_uuid_t";
807  }
808  break;
809  case t_base_type::TYPE_IPADDR:
810  if (value->get_type() != t_const_value::CV_STRING) {
811  throw "type error: const \"" + name + "\" was declared as ipaddr";
812  }
813  break;
814 #endif
816  if (value->get_type() != t_const_value::CV_INTEGER) {
817  throw "type error: const \"" + name + "\" was declared as bool";
818  }
819  break;
821  if (value->get_type() != t_const_value::CV_INTEGER) {
822  throw "type error: const \"" + name + "\" was declared as byte";
823  }
824  break;
826  if (value->get_type() != t_const_value::CV_INTEGER) {
827  throw "type error: const \"" + name + "\" was declared as i16";
828  }
829  break;
831  if (value->get_type() != t_const_value::CV_INTEGER) {
832  throw "type error: const \"" + name + "\" was declared as i32";
833  }
834  break;
836  if (value->get_type() != t_const_value::CV_INTEGER) {
837  throw "type error: const \"" + name + "\" was declared as i64";
838  }
839  break;
841  if (value->get_type() != t_const_value::CV_INTEGER &&
842  value->get_type() != t_const_value::CV_DOUBLE) {
843  throw "type error: const \"" + name + "\" was declared as double";
844  }
845  break;
846  default:
847  throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase) + name;
848  }
849  } else if (type->is_enum()) {
850  if (value->get_type() != t_const_value::CV_IDENTIFIER) {
851  throw "type error: const \"" + name + "\" was declared as enum";
852  }
853 
854  // see if there's a dot in the identifier
855  std::string name_portion = value->get_identifier_name();
856 
857  const vector<t_enum_value*>& enum_values = ((t_enum*)type)->get_constants();
858  vector<t_enum_value*>::const_iterator c_iter;
859  bool found = false;
860 
861  for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
862  if ((*c_iter)->get_name() == name_portion) {
863  found = true;
864  break;
865  }
866  }
867  if (!found) {
868  throw "type error: const " + name + " was declared as type "
869  + type->get_name() + " which is an enum, but "
870  + value->get_identifier() + " is not a valid value for that enum";
871  }
872  } else if (type->is_struct() || type->is_xception()) {
873  if (value->get_type() != t_const_value::CV_MAP) {
874  throw "type error: const \"" + name + "\" was declared as struct/xception";
875  }
876  const vector<t_field*>& fields = ((t_struct*)type)->get_members();
877  vector<t_field*>::const_iterator f_iter;
878 
879  const map<t_const_value*, t_const_value*>& val = value->get_map();
880  map<t_const_value*, t_const_value*>::const_iterator v_iter;
881  for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
882  if (v_iter->first->get_type() != t_const_value::CV_STRING) {
883  throw "type error: " + name + " struct key must be string";
884  }
885  t_type* field_type = NULL;
886  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
887  if ((*f_iter)->get_name() == v_iter->first->get_string()) {
888  field_type = (*f_iter)->get_type();
889  }
890  }
891  if (field_type == NULL) {
892  throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
893  }
894 
895  validate_const_rec(name + "." + v_iter->first->get_string(), field_type, v_iter->second);
896  }
897  } else if (type->is_map()) {
898  t_type* k_type = ((t_map*)type)->get_key_type();
899  t_type* v_type = ((t_map*)type)->get_val_type();
900  const map<t_const_value*, t_const_value*>& val = value->get_map();
901  map<t_const_value*, t_const_value*>::const_iterator v_iter;
902  for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
903  validate_const_rec(name + "<key>", k_type, v_iter->first);
904  validate_const_rec(name + "<val>", v_type, v_iter->second);
905  }
906  } else if (type->is_list() || type->is_set()) {
907  t_type* e_type;
908  if (type->is_list()) {
909  e_type = ((t_list*)type)->get_elem_type();
910  } else {
911  e_type = ((t_set*)type)->get_elem_type();
912  }
913  const vector<t_const_value*>& val = value->get_list();
914  vector<t_const_value*>::const_iterator v_iter;
915  for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
916  validate_const_rec(name + "<elem>", e_type, *v_iter);
917  }
918  }
919 }
920 
925  validate_const_rec(c->get_name(), c->get_type(), c->get_value());
926 }
927 
932  validate_const_rec(field->get_name(), field->get_type(), cv);
933 }
934 
938 bool validate_throws(t_struct* throws) {
939  const vector<t_field*>& members = throws->get_members();
940  vector<t_field*>::const_iterator m_iter;
941  for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
942  if (!t_generator::get_true_type((*m_iter)->get_type())->is_xception()) {
943  return false;
944  }
945  }
946  return true;
947 }
948 
952 void parse(t_program* program, t_program* parent_program) {
953  // Get scope file path
954  string path = program->get_path();
955 
956  // Set current dir global, which is used in the include_file function
957  g_curdir = directory_name(path);
958  g_curpath = path;
959 
960  // Open the file
961  yyin = fopen(path.c_str(), "r");
962  if (yyin == 0) {
963  failure("Could not open input file: \"%s\"", path.c_str());
964  }
965 
966  // Create new scope and scan for includes
967  pverbose("Scanning %s for includes\n", path.c_str());
969  g_program = program;
970  g_scope = program->scope();
971  try {
972  yylineno = 1;
973  if (yyparse() != 0) {
974  failure("Parser error during include pass.");
975  }
976  } catch (string x) {
977  failure(x.c_str());
978  }
979  fclose(yyin);
980 
981  // Recursively parse all the include programs
982  vector<t_program*>& includes = program->get_includes();
983  vector<t_program*>::iterator iter;
984  for (iter = includes.begin(); iter != includes.end(); ++iter) {
985  parse(*iter, program);
986  }
987 
988  // reset program doctext status before parsing a new file
990 
991  // Parse the program file
993  g_program = program;
994  g_scope = program->scope();
995  g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL;
996  g_parent_prefix = program->get_name() + ".";
997  g_curpath = path;
998  yyin = fopen(path.c_str(), "r");
999  if (yyin == 0) {
1000  failure("Could not open input file: \"%s\"", path.c_str());
1001  }
1002  pverbose("Parsing %s for types\n", path.c_str());
1003  yylineno = 1;
1004  try {
1005  if (yyparse() != 0) {
1006  failure("Parser error during types pass.");
1007  }
1008  } catch (string x) {
1009  failure(x.c_str());
1010  }
1011  fclose(yyin);
1012 }
1013 
1017 void generate(t_program* program, const vector<string>& generator_strings) {
1018  // Oooohh, recursive code generation, hot!!
1019  if (gen_recurse) {
1020  const vector<t_program*>& includes = program->get_includes();
1021  for (size_t i = 0; i < includes.size(); ++i) {
1022  // Propogate output path from parent to child programs
1023  includes[i]->set_out_path(program->get_out_path(), program->is_out_path_absolute());
1024 
1025  generate(includes[i], generator_strings);
1026  }
1027  }
1028 
1029  // Generate code!
1030  try {
1031  pverbose("Program: %s\n", program->get_path().c_str());
1032 
1033  // Compute fingerprints.
1034  generate_all_fingerprints(program);
1035 
1036  if (dump_docs) {
1037  dump_docstrings(program);
1038  }
1039 
1040  vector<string>::const_iterator iter;
1041  for (iter = generator_strings.begin(); iter != generator_strings.end(); ++iter) {
1042  t_generator* generator = t_generator_registry::get_generator(program, *iter);
1043 
1044  if (generator == NULL) {
1045  pwarning(1, "Unable to get a generator for \"%s\".\n", iter->c_str());
1046  } else {
1047  pverbose("Generating \"%s\"\n", iter->c_str());
1048  generator->generate_program();
1049  delete generator;
1050  }
1051  }
1052 
1053  } catch (string s) {
1054  printf("Error: %s\n", s.c_str());
1055 #ifdef SANDESH
1056  failure("Error: %s\n", s.c_str());
1057 #endif
1058  } catch (const char* exc) {
1059  printf("Error: %s\n", exc);
1060 #ifdef SANDESH
1061  failure("Error: %s\n", exc);
1062 #endif
1063  }
1064 }
1065 
1070 int main(int argc, char** argv) {
1071  int i;
1072  std::string out_path;
1073  bool out_path_is_absolute = false;
1074 
1075  // Setup time string
1076  time_t now = time(NULL);
1077  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  if (argc < 2) {
1082  usage();
1083  }
1084 
1085  vector<string> generator_strings;
1086 
1087  // Set the current path to a dummy value to make warning messages clearer.
1088  g_curpath = "arguments";
1089 
1090  // Hacky parameter handling... I didn't feel like using a library sorry!
1091  for (i = 1; i < argc-1; i++) {
1092  char* arg;
1093 
1094  char *saveptr;
1095  arg = strtok_r(argv[i], " ", &saveptr);
1096  while (arg != NULL) {
1097  // Treat double dashes as single dashes
1098  if (arg[0] == '-' && arg[1] == '-') {
1099  ++arg;
1100  }
1101 
1102  if (strcmp(arg, "-version") == 0) {
1103  version();
1104  exit(1);
1105  } else if (strcmp(arg, "-debug") == 0) {
1106  g_debug = 1;
1107  } else if (strcmp(arg, "-nowarn") == 0) {
1108  g_warn = 0;
1109  } else if (strcmp(arg, "-strict") == 0) {
1110  g_strict = 255;
1111  g_warn = 2;
1112  } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0 ) {
1113  g_verbose = 1;
1114  } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0 ) {
1115  gen_recurse = true;
1116  } else if (strcmp(arg, "-allow-neg-keys") == 0) {
1117  g_allow_neg_field_keys = true;
1118  } else if (strcmp(arg, "-allow-64bit-consts") == 0) {
1119  g_allow_64bit_consts = true;
1120  } else if (strcmp(arg, "-gen") == 0) {
1121  arg = argv[++i];
1122  if (arg == NULL) {
1123  fprintf(stderr, "!!! Missing generator specification\n");
1124  usage();
1125  }
1126  generator_strings.push_back(arg);
1127  } else if (strcmp(arg, "-dense") == 0) {
1128  gen_dense = true;
1129  } else if (strcmp(arg, "-cpp") == 0) {
1130  gen_cpp = true;
1131  } else if (strcmp(arg, "-javabean") == 0) {
1132  gen_javabean = true;
1133  } else if (strcmp(arg, "-java") == 0) {
1134  gen_java = true;
1135  } else if (strcmp(arg, "-php") == 0) {
1136  gen_php = true;
1137  } else if (strcmp(arg, "-phpi") == 0) {
1138  gen_phpi = true;
1139  } else if (strcmp(arg, "-phps") == 0) {
1140  gen_php = true;
1141  gen_phps = true;
1142  } else if (strcmp(arg, "-phpl") == 0) {
1143  gen_php = true;
1144  gen_phps = false;
1145  } else if (strcmp(arg, "-phpa") == 0) {
1146  gen_php = true;
1147  gen_phps = false;
1148  gen_phpa = true;
1149  } else if (strcmp(arg, "-phpo") == 0) {
1150  gen_php = true;
1151  gen_phpo = true;
1152  } else if (strcmp(arg, "-rest") == 0) {
1153  gen_rest = true;
1154  } else if (strcmp(arg, "-py") == 0) {
1155  gen_py = true;
1156  } else if (strcmp(arg, "-pyns") == 0) {
1157  gen_py = true;
1158  gen_py_newstyle = true;
1159  } else if (strcmp(arg, "-rb") == 0) {
1160  gen_rb = true;
1161  } else if (strcmp(arg, "-xsd") == 0) {
1162  gen_xsd = true;
1163  } else if (strcmp(arg, "-perl") == 0) {
1164  gen_perl = true;
1165  } else if (strcmp(arg, "-erl") == 0) {
1166  gen_erl = true;
1167  } else if (strcmp(arg, "-ocaml") == 0) {
1168  gen_ocaml = true;
1169  } else if (strcmp(arg, "-hs") == 0) {
1170  gen_hs = true;
1171  } else if (strcmp(arg, "-cocoa") == 0) {
1172  gen_cocoa = true;
1173  } else if (strcmp(arg, "-st") == 0) {
1174  gen_st = true;
1175  } else if (strcmp(arg, "-csharp") == 0) {
1176  gen_csharp = true;
1177  } else if (strcmp(arg, "-delphi") == 0) {
1178  gen_delphi = true;
1179  } else if (strcmp(arg, "-cpp_use_include_prefix") == 0) {
1180  g_cpp_use_include_prefix = true;
1181  } else if (strcmp(arg, "-I") == 0) {
1182  // An argument of "-I\ asdf" is invalid and has unknown results
1183  arg = argv[++i];
1184 
1185  if (arg == NULL) {
1186  fprintf(stderr, "!!! Missing Include directory\n");
1187  usage();
1188  }
1189  g_incl_searchpath.push_back(arg);
1190  } else if ((strcmp(arg, "-o") == 0) || (strcmp(arg, "-out") == 0)) {
1191  out_path_is_absolute = (strcmp(arg, "-out") == 0) ? true : false;
1192 
1193  arg = argv[++i];
1194  if (arg == NULL) {
1195  fprintf(stderr, "-o: missing output directory\n");
1196  usage();
1197  }
1198  out_path = arg;
1199 
1200  struct stat sb;
1201  if (stat(out_path.c_str(), &sb) < 0) {
1202  fprintf(stderr, "Output directory %s is unusable: %s\n", out_path.c_str(), strerror(errno));
1203  return -1;
1204  }
1205  if (! S_ISDIR(sb.st_mode)) {
1206  fprintf(stderr, "Output directory %s exists but is not a directory\n", out_path.c_str());
1207  return -1;
1208  }
1209  } else {
1210  fprintf(stderr, "!!! Unrecognized option: %s\n", arg);
1211  usage();
1212  }
1213 
1214  // Tokenize more
1215  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  if (strcmp(argv[argc-1], "-version") == 0) {
1221  version();
1222  exit(1);
1223  }
1224 
1225  // TODO(dreiss): Delete these when everyone is using the new hotness.
1226  if (gen_cpp) {
1227  pwarning(1, "-cpp is deprecated. Use --gen cpp");
1228  string gen_string = "cpp:";
1229  if (gen_dense) {
1230  gen_string.append("dense,");
1231  }
1233  gen_string.append("include_prefix,");
1234  }
1235  generator_strings.push_back(gen_string);
1236  }
1237  if (gen_java) {
1238  pwarning(1, "-java is deprecated. Use --gen java");
1239  generator_strings.push_back("java");
1240  }
1241  if (gen_javabean) {
1242  pwarning(1, "-javabean is deprecated. Use --gen java:beans");
1243  generator_strings.push_back("java:beans");
1244  }
1245  if (gen_csharp) {
1246  pwarning(1, "-csharp is deprecated. Use --gen csharp");
1247  generator_strings.push_back("csharp");
1248  }
1249  if (gen_delphi) {
1250  pwarning(1, "-delphi is deprecated. Use --gen delphi");
1251  generator_strings.push_back("delphi");
1252  }
1253  if (gen_py) {
1254  pwarning(1, "-py is deprecated. Use --gen py");
1255  generator_strings.push_back("py");
1256  }
1257  if (gen_rb) {
1258  pwarning(1, "-rb is deprecated. Use --gen rb");
1259  generator_strings.push_back("rb");
1260  }
1261  if (gen_perl) {
1262  pwarning(1, "-perl is deprecated. Use --gen perl");
1263  generator_strings.push_back("perl");
1264  }
1265  if (gen_php || gen_phpi) {
1266  pwarning(1, "-php is deprecated. Use --gen php");
1267  string gen_string = "php:";
1268  if (gen_phpi) {
1269  gen_string.append("inlined,");
1270  } else if(gen_phps) {
1271  gen_string.append("server,");
1272  } else if(gen_phpa) {
1273  gen_string.append("autoload,");
1274  } else if(gen_phpo) {
1275  gen_string.append("oop,");
1276  } else if(gen_rest) {
1277  gen_string.append("rest,");
1278  }
1279  generator_strings.push_back(gen_string);
1280  }
1281  if (gen_cocoa) {
1282  pwarning(1, "-cocoa is deprecated. Use --gen cocoa");
1283  generator_strings.push_back("cocoa");
1284  }
1285  if (gen_erl) {
1286  pwarning(1, "-erl is deprecated. Use --gen erl");
1287  generator_strings.push_back("erl");
1288  }
1289  if (gen_st) {
1290  pwarning(1, "-st is deprecated. Use --gen st");
1291  generator_strings.push_back("st");
1292  }
1293  if (gen_ocaml) {
1294  pwarning(1, "-ocaml is deprecated. Use --gen ocaml");
1295  generator_strings.push_back("ocaml");
1296  }
1297  if (gen_hs) {
1298  pwarning(1, "-hs is deprecated. Use --gen hs");
1299  generator_strings.push_back("hs");
1300  }
1301  if (gen_xsd) {
1302  pwarning(1, "-xsd is deprecated. Use --gen xsd");
1303  generator_strings.push_back("xsd");
1304  }
1305 
1306  // You gotta generate something!
1307  if (generator_strings.empty()) {
1308  fprintf(stderr, "!!! No output language(s) specified\n\n");
1309  usage();
1310  }
1311 
1312  // Real-pathify it
1313  char rp[PATH_MAX];
1314  if (argv[i] == NULL) {
1315  fprintf(stderr, "!!! Missing file name\n");
1316  usage();
1317  }
1318  if (saferealpath(argv[i], rp) == NULL) {
1319  failure("Could not open input file with realpath: %s", argv[i]);
1320  }
1321  string input_file(rp);
1322 
1323  // Instance of the global parse tree
1324  t_program* program = new t_program(input_file);
1325  if (out_path.size()) {
1326  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  string input_filename = argv[i];
1332  string include_prefix;
1333 
1334  string::size_type last_slash = string::npos;
1335  if ((last_slash = input_filename.rfind("/")) != string::npos) {
1336  include_prefix = input_filename.substr(0, last_slash);
1337  }
1338 
1339  program->set_include_prefix(include_prefix);
1340 
1341  // Initialize global types
1345  ((t_base_type*)g_type_binary)->set_binary(true);
1347  ((t_base_type*)g_type_slist)->set_string_list(true);
1354 #ifdef SANDESH
1355  g_type_u16 = new t_base_type("u16", t_base_type::TYPE_U16);
1356  g_type_u32 = new t_base_type("u32", t_base_type::TYPE_U32);
1357  g_type_u64 = new t_base_type("u64", t_base_type::TYPE_U64);
1358  g_type_ipv4 = new t_base_type("ipv4", t_base_type::TYPE_IPV4);
1359  g_type_ipaddr = new t_base_type("ipaddr", t_base_type::TYPE_IPADDR);
1360  g_type_xml = new t_base_type("xml", t_base_type::TYPE_XML);
1361  g_type_uuid_t = new t_base_type("ct_uuid_t", t_base_type::TYPE_UUID);
1362  g_type_static_const_string = new t_base_type("static const string", t_base_type::TYPE_STATIC_CONST_STRING);
1363  g_type_sandesh_system = new t_base_type("system", t_base_type::TYPE_SANDESH_SYSTEM);
1364  g_type_sandesh_request = new t_base_type("request", t_base_type::TYPE_SANDESH_REQUEST);
1365  g_type_sandesh_response = new t_base_type("response", t_base_type::TYPE_SANDESH_RESPONSE);
1366  g_type_sandesh_trace = new t_base_type("trace", t_base_type::TYPE_SANDESH_TRACE);
1367  g_type_sandesh_trace_object = new t_base_type("traceobject", t_base_type::TYPE_SANDESH_TRACE_OBJECT);
1368  g_type_sandesh_buffer = new t_base_type("buffer", t_base_type::TYPE_SANDESH_BUFFER);
1369  g_type_sandesh_uve = new t_base_type("uve", t_base_type::TYPE_SANDESH_UVE);
1370  g_type_sandesh_dynamic_uve = new t_base_type("dynamicuve", t_base_type::TYPE_SANDESH_DYNAMIC_UVE);
1371  g_type_sandesh_alarm = new t_base_type("alarm", t_base_type::TYPE_SANDESH_ALARM);
1372  g_type_sandesh_object = new t_base_type("object", t_base_type::TYPE_SANDESH_OBJECT);
1373  g_type_sandesh_flow = new t_base_type("flow", t_base_type::TYPE_SANDESH_FLOW);
1374  g_type_sandesh_session = new t_base_type("session", t_base_type::TYPE_SANDESH_SESSION);
1375 #endif
1376 
1377  // Parse it!
1378  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  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  yylineno = 1;
1386  // Generate it!
1387  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  delete program;
1394  delete g_type_void;
1395  delete g_type_string;
1396  delete g_type_bool;
1397  delete g_type_byte;
1398  delete g_type_i16;
1399  delete g_type_i32;
1400  delete g_type_i64;
1401  delete g_type_double;
1402 #ifdef SANDESH
1403  delete g_type_u16;
1404  delete g_type_u32;
1405  delete g_type_u64;
1406  delete g_type_ipv4;
1407  delete g_type_ipaddr;
1408  delete g_type_static_const_string;
1409  delete g_type_sandesh_system;
1410  delete g_type_sandesh_request;
1411  delete g_type_sandesh_response;
1412  delete g_type_sandesh_trace;
1413  delete g_type_sandesh_trace_object;
1414  delete g_type_sandesh_buffer;
1415  delete g_type_sandesh_uve;
1416  delete g_type_sandesh_dynamic_uve;
1417  delete g_type_sandesh_alarm;
1418  delete g_type_sandesh_object;
1419  delete g_type_sandesh_flow;
1420  delete g_type_sandesh_session;
1421  delete g_type_xml;
1422  delete g_type_uuid_t;
1423 
1424 #endif
1425 
1426  // Finished
1427  return 0;
1428 }
static std::string t_base_name(t_base tbase)
Definition: t_base_type.h:221
const std::vector< t_const_value * > & get_list() const
std::string get_identifier() const
std::string get_identifier_name() const
t_const_value_type get_type() const
const std::map< t_const_value *, t_const_value * > & get_map() const
t_type * get_type() const
Definition: t_const.h:43
std::string get_name() const
Definition: t_const.h:47
t_const_value * get_value() const
Definition: t_const.h:51
bool has_doc()
Definition: t_doc.h:44
const std::string & get_doc() const
Definition: t_doc.h:40
Definition: t_enum.h:33
const std::string & get_name() const
Definition: t_field.h:98
t_type * get_type() const
Definition: t_field.h:94
static gen_map_t & get_generator_map()
Definition: t_generator.cc:177
static t_generator * get_generator(t_program *program, const std::string &options)
Definition: t_generator.cc:140
std::map< std::string, t_generator_factory * > gen_map_t
virtual void generate_program()
Definition: t_generator.cc:33
static t_type * get_true_type(t_type *type)
Definition: t_generator.h:271
Definition: t_list.h:32
Definition: t_map.h:33
const std::vector< t_program * > & get_includes() const
Definition: t_program.h:129
void set_include_prefix(std::string include_prefix)
Definition: t_program.h:172
const std::vector< t_service * > & get_services() const
Definition: t_program.h:110
bool is_out_path_absolute()
Definition: t_program.h:92
const std::string & get_path() const
Definition: t_program.h:86
const std::vector< t_struct * > & get_structs() const
Definition: t_program.h:107
const std::vector< t_const * > & get_consts() const
Definition: t_program.h:106
const std::string & get_out_path() const
Definition: t_program.h:89
const std::vector< t_struct * > & get_xceptions() const
Definition: t_program.h:108
const std::string & get_name() const
Definition: t_program.h:95
t_scope * scope()
Definition: t_program.h:147
const std::vector< t_enum * > & get_enums() const
Definition: t_program.h:105
void set_out_path(std::string out_path, bool out_path_is_absolute)
Definition: t_program.h:131
const std::vector< t_typedef * > & get_typedefs() const
Definition: t_program.h:104
Definition: t_set.h:32
const members_type & get_members()
virtual void generate_fingerprint()
Definition: t_type.h:51
virtual void generate_fingerprint()
Definition: parse.cc:13
virtual bool is_xception() const
Definition: t_type.h:77
virtual const std::string & get_name() const
Definition: t_type.h:59
PARSE_MODE
Definition: globals.h:57
@ PROGRAM
Definition: globals.h:59
@ INCLUDES
Definition: globals.h:58
PROGDOCTEXT_STATUS
Definition: globals.h:149
@ STILL_CANDIDATE
Definition: globals.h:151
@ NO_PROGRAM_DOCTEXT
Definition: globals.h:154
@ ABSOLUTELY_SURE
Definition: globals.h:153
@ INVALID
Definition: globals.h:150
uint8_t type
Definition: load_balance.h:2
FILE * yyin
int yylineno
int yyparse(void)
char yytext[]
void validate_const_type(t_const *c)
char * clean_up_doctext(char *doctext)
void pwarning(int level, const char *fmt,...)
bool validate_throws(t_struct *throws)
void dump_docstrings(t_program *program)
void pverbose(const char *fmt,...)
void yyerror(const char *fmt,...)
void validate_const_rec(std::string name, t_type *type, t_const_value *value)
char * saferealpath(const char *path, char *resolved_path)
string directory_name(string filename)
string program_name(string filename)
void validate_field_value(t_field *field, t_const_value *cv)
void generate_all_fingerprints(t_program *program)
void parse(t_program *program, t_program *parent_program)
PROGDOCTEXT_STATUS g_program_doctext_status
void generate(t_program *program, const vector< string > &generator_strings)
string include_file(string filename)
vector< string > g_incl_searchpath
void failure(const char *fmt,...)
void pdebug(const char *fmt,...)
#define SANDESH_VERSION
Definition: version.h:1