Reject "llvm." as a function name
[oota-llvm.git] / tools / gccld / util.cpp
1 //===- util.cpp - Utility functions ---------------------------------------===//
2 //
3 // This file contains utility functions for gccld.  It essentially holds
4 // anything from the original gccld.cpp source that was either incidental
5 // or not inlined.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/Module.h"
10 #include "Config/string.h"
11
12 #include <fstream>
13 #include <string>
14 #include <set>
15
16 //
17 // Function: PrintAndReturn ()
18 //
19 // Description:
20 //  Prints a message (usually error message) to standard error (stderr) and
21 //  returns a value usable for an exit status.
22 //
23 // Inputs:
24 //  progname - The name of the program (i.e. argv[0]).
25 //  Message  - The message to print to standard error.
26 //  Extra    - Extra information to print between the program name and thei
27 //             message.  It is optional.
28 //
29 // Outputs:
30 //  None.
31 //
32 // Return value:
33 //  Returns a value that can be used as the exit status (i.e. for exit()).
34 //
35 int
36 PrintAndReturn (const char *progname,
37                 const std::string &Message,
38                 const std::string &Extra = "")
39 {
40   std::cerr << progname << Extra << ": " << Message << "\n";
41   return 1;
42 }
43
44 //
45 // Function: IsArchive ()
46 //
47 // Description:
48 //  Determine if the specified file is an ar archive.  It determines this by
49 //  checking the magic string at the beginning of the file.
50 //
51 // Inputs:
52 //  filename - A C++ string containing the name of the file.
53 //
54 // Outputs:
55 //  None.
56 //
57 // Return value:
58 //  TRUE  - The file is an archive.
59 //  FALSE - The file is not an archive.
60 //
61 bool
62 IsArchive (const std::string &filename)
63 {
64   std::string ArchiveMagic("!<arch>\012");
65   char buf[1 + ArchiveMagic.size()];
66
67   std::ifstream f(filename.c_str());
68   f.read(buf, ArchiveMagic.size());
69   buf[ArchiveMagic.size()] = '\0';
70   return ArchiveMagic == buf;
71 }
72
73 //
74 // Function: GetAllDefinedSymbols ()
75 //
76 // Description:
77 //  Find all of the defined symbols in the specified module.
78 //
79 // Inputs:
80 //  M - The module in which to find defined symbols.
81 //
82 // Outputs:
83 //  DefinedSymbols - A set of C++ strings that will contain the name of all
84 //                   defined symbols.
85 //
86 // Return value:
87 //  None.
88 //
89 void
90 GetAllDefinedSymbols (Module *M, std::set<std::string> &DefinedSymbols)
91 {
92   for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
93     if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage())
94       DefinedSymbols.insert(I->getName());
95   for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
96     if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage())
97       DefinedSymbols.insert(I->getName());
98 }
99
100 //
101 // Function: GetAllUndefinedSymbols ()
102 //
103 // Description:
104 //  This calculates the set of undefined symbols that still exist in an LLVM
105 //  module.  This is a bit tricky because there may be two symbols with the
106 //  same name but different LLVM types that will be resolved to each other but
107 //  aren't currently (thus we need to treat it as resolved).
108 //
109 // Inputs:
110 //  M - The module in which to find undefined symbols.
111 //
112 // Outputs:
113 //  UndefinedSymbols - A set of C++ strings containing the name of all
114 //                     undefined symbols.
115 //
116 // Return value:
117 //  None.
118 //
119 void
120 GetAllUndefinedSymbols(Module *M, std::set<std::string> &UndefinedSymbols)
121 {
122   std::set<std::string> DefinedSymbols;
123   UndefinedSymbols.clear();   // Start out empty
124   
125   for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
126     if (I->hasName()) {
127       if (I->isExternal())
128         UndefinedSymbols.insert(I->getName());
129       else if (!I->hasInternalLinkage())
130         DefinedSymbols.insert(I->getName());
131     }
132   for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
133     if (I->hasName()) {
134       if (I->isExternal())
135         UndefinedSymbols.insert(I->getName());
136       else if (!I->hasInternalLinkage())
137         DefinedSymbols.insert(I->getName());
138     }
139   
140   // Prune out any defined symbols from the undefined symbols set...
141   for (std::set<std::string>::iterator I = UndefinedSymbols.begin();
142        I != UndefinedSymbols.end(); )
143     if (DefinedSymbols.count(*I))
144       UndefinedSymbols.erase(I++);  // This symbol really is defined!
145     else
146       ++I; // Keep this symbol in the undefined symbols list
147 }
148
149 //
150 //
151 // Function: copy_env()
152 //
153 // Description:
154 //      This function takes an array of environment variables and makes a
155 //      copy of it.  This copy can then be manipulated any way the caller likes
156 //  without affecting the process's real environment.
157 //
158 // Inputs:
159 //  envp - An array of C strings containing an environment.
160 //
161 // Outputs:
162 //  None.
163 //
164 // Return value:
165 //  NULL - An error occurred.
166 //
167 //  Otherwise, a pointer to a new array of C strings is returned.  Every string
168 //  in the array is a duplicate of the one in the original array (i.e. we do
169 //  not copy the char *'s from one array to another).
170 //
171 char **
172 copy_env (char ** const envp)
173 {
174   // The new environment list
175   char ** newenv;
176
177   // The number of entries in the old environment list
178   int entries;
179
180   //
181   // Count the number of entries in the old list;
182   //
183   for (entries = 0; envp[entries] != NULL; entries++)
184   {
185     ;
186   }
187
188   //
189   // Add one more entry for the NULL pointer that ends the list.
190   //
191   ++entries;
192
193   //
194   // If there are no entries at all, just return NULL.
195   //
196   if (entries == 0)
197   {
198     return NULL;
199   }
200
201   //
202   // Allocate a new environment list.
203   //
204   if ((newenv = new (char *) [entries]) == NULL)
205   {
206     return NULL;
207   }
208
209   //
210   // Make a copy of the list.  Don't forget the NULL that ends the list.
211   //
212   entries = 0;
213   while (envp[entries] != NULL)
214   {
215     newenv[entries] = new char[strlen (envp[entries]) + 1];
216     strcpy (newenv[entries], envp[entries]);
217     ++entries;
218   }
219   newenv[entries] = NULL;
220
221   return newenv;
222 }
223
224
225 //
226 // Function: remove_env()
227 //
228 // Description:
229 //      Remove the specified environment variable from the environment array.
230 //
231 // Inputs:
232 //      name - The name of the variable to remove.  It cannot be NULL.
233 //      envp - The array of environment variables.  It cannot be NULL.
234 //
235 // Outputs:
236 //      envp - The pointer to the specified variable name is removed.
237 //
238 // Return value:
239 //      None.
240 //
241 // Notes:
242 //  This is mainly done because functions to remove items from the environment
243 //  are not available across all platforms.  In particular, Solaris does not
244 //  seem to have an unsetenv() function or a setenv() function (or they are
245 //  undocumented if they do exist).
246 //
247 void
248 remove_env (const char * name, char ** const envp)
249 {
250   // Pointer for scanning arrays
251   register char * p;
252
253   // Index for selecting elements of the environment array
254   register int index;
255
256   for (index=0; envp[index] != NULL; index++)
257   {
258     //
259     // Find the first equals sign in the array and make it an EOS character.
260     //
261     p = strchr (envp[index], '=');
262     if (p == NULL)
263     {
264       continue;
265     }
266     else
267     {
268       *p = '\0';
269     }
270
271     //
272     // Compare the two strings.  If they are equal, zap this string.
273     // Otherwise, restore it.
274     //
275     if (!strcmp (name, envp[index]))
276     {
277       *envp[index] = '\0';
278     }
279     else
280     {
281       *p = '=';
282     }
283   }
284
285   return;
286 }
287