PIPS
util.c
Go to the documentation of this file.
1 /*
2 
3  $Id: util.c$
4 
5  Copyright 1989-2016 MINES ParisTech
6 
7  This file is part of PIPS.
8 
9  PIPS is free software: you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  any later version.
13 
14  PIPS is distributed in the hope that it will be useful, but WITHOUT ANY
15  WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  FITNESS FOR A PARTICULAR PURPOSE.
17 
18  See the GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with PIPS. If not, see <http://www.gnu.org/licenses/>.
22 
23 */
24 #ifdef HAVE_CONFIG_H
25  #include "pips_config.h"
26 #endif
27 
28 #include "genC.h"
29 
30 #include "misc.h"
31 #include "naming.h"
32 #include "pipsdbm.h"
33 
34 /* returns whether there is a main in the database
35  */
36 
38 {
39  bool some_main_p = false;
41  "/", MAIN_FILE_NAMES, NULL));
42  FILE * fm = fopen(main_list, "r");
43  if(fm!=NULL) {
44  some_main_p = true;
45  fclose(fm);
46  }
47  free(main_list);
48  return some_main_p;
49 }
50 
51 
52 /* Return the local name of the main module if it is available,
53  * or the local name of any module by default.
54  *
55  * Hopefully, the module names are sorted and the same module of rank
56  * 0 is always returned.
57  *
58  * A new string is allocated.
59  *
60  * Up to a memory leak:
61  * entity get_main_entity(void) == module_name_to_entity(get_main_entity_name())
62  */
64 {
65  string mn = string_undefined;
66  char buffer[MAXIMAL_MODULE_NAME_SIZE]; // defined in naming.h
68  "/", MAIN_FILE_NAMES, NULL));
69  FILE * fm = fopen(main_list, "r");
70  buffer[0] = 0;
71  if (fm!=NULL) {
72  // FI: we assume here that we have only one main function
74  fclose(fm);
75  // Drop the final LF
76  mn = strndup(buffer, strlen(buffer)-1);
77  }
78  else {
79  // FI: a module list is an array ?
80  gen_array_t modules = db_get_module_list();
81  int nmodules = gen_array_nitems(modules);
82 
83  pips_assert("some modules in the program", nmodules>0);
84  /* ??? some default if there is no main...
85  *
86  * This has an impact on interprocedural analysis and especially
87  * semantics and works well if only one function is analyzed.
88  */
89  pips_user_warning("No main function found, use \"%s\" instead.\n",
90  gen_array_item(modules,0));
91  mn = strdup(gen_array_item(modules, 0));
92 
93  gen_array_full_free(modules);
94  }
95  return mn;
96 }
97 
98 /** @} */
size_t gen_array_nitems(const gen_array_t a)
Definition: array.c:131
void gen_array_full_free(gen_array_t a)
Definition: array.c:77
void * gen_array_item(const gen_array_t a, size_t i)
Definition: array.c:143
char * safe_fgets(char *s, int n, FILE *stream, char *filename)
Definition: file.c:170
void free(void *)
gen_array_t db_get_module_list(void)
Get an array of all the modules (functions, procedures and compilation units) of a workspace.
Definition: database.c:1266
#define pips_user_warning
Definition: misc-local.h:146
#define pips_assert(what, predicate)
common macros, two flavors depending on NDEBUG
Definition: misc-local.h:172
#define MAXIMAL_MODULE_NAME_SIZE
In C, the module name may include file names, the compilation unit name and the user name of the func...
Definition: naming-local.h:112
string concatenate(const char *,...)
Return the concatenation of the given strings.
Definition: string.c:183
#define string_undefined
Definition: newgen_types.h:40
#define MAIN_FILE_NAMES
Name of the file containing the names of the main procedures.
Definition: pipsdbm-local.h:41
string get_main_entity_name(void)
Return the local name of the main module if it is available, or the local name of any module by defau...
Definition: util.c:63
bool some_main_entity_p(void)
returns whether there is a main in the database
Definition: util.c:37
string db_get_current_workspace_directory(void)
Definition: workspace.c:96
char * strdup()
static string buffer
Definition: string.c:113
char * strndup(char const *s, size_t n)
A replacement function, for systems that lack strndup.
Definition: strndup.c:26