diagnostics.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 #include <stdarg.h>
00068 #include <errno.h>
00069 #include <stdio.h>
00070
00071
00072
00073 #include "Open64BasicTypes.h"
00074 #include "file_util.h"
00075
00076
00077
00078 #include "diagnostics.h"
00079
00080
00081
00082 int DBG_LVL_PUB = 0;
00083
00084 static char Diag_Phase_Name[80] = "";
00085 static FILE *Diag_File = NULL;
00086 static int Diag_Max_Diags = 10;
00087 static int Diag_Warn_Count = 0;
00088 static const char *Diag_File_Location = NULL;
00089 static int Diag_Line_Location = 0;
00090
00091 static const char *Diag_Msg[DIAG_LAST+1];
00092
00093
00094 void
00095 Diag_Init(void)
00096 {
00097 int diag;
00098
00099
00100
00101 for (diag = DIAG_FIRST; diag <= DIAG_LAST; diag++)
00102 Diag_Msg[diag] = "*** Unknown diagnostics code ***";
00103
00104
00105
00106 Diag_Msg[DIAG_A_STRING] = "%s";
00107 Diag_Msg[DIAG_UNIMPLEMENTED] = "TODO: Unimplemented feature: %s";
00108 Diag_Msg[DIAG_UNKNOWN_CMD_LINE_OPTION] = "Unknown command-line option: %s";
00109 Diag_Msg[DIAG_CANNOT_OPEN_FILE] = "Cannot open file (%s), errno=%d";
00110 Diag_Msg[DIAG_CANNOT_CLOSE_FILE] = "Cannot close file (%s), errno=%d";
00111
00112 Diag_Msg[DIAG_W2F_CANNOT_HANDLE_OPC] =
00113 "cannot handle opcode %s (%d)";
00114 Diag_Msg[DIAG_W2F_UNEXPECTED_OPC] =
00115 "unexpected opcode in %s()";
00116 Diag_Msg[DIAG_W2F_UNEXPECTED_IOS] =
00117 "unexpected IO statement kind %s in %s()";
00118 Diag_Msg[DIAG_W2F_UNEXPECTED_IOU] =
00119 "unexpected IO unit kind %s in %s()";
00120 Diag_Msg[DIAG_W2F_UNEXPECTED_IOF] =
00121 "unexpected IO format kind %s in %s()";
00122 Diag_Msg[DIAG_W2F_UNEXPECTED_IOC] =
00123 "unexpected IO control kind %s in %s()";
00124 Diag_Msg[DIAG_W2F_UNEXPECTED_IOL] =
00125 "unexpected IO list kind %s in %s()";
00126 Diag_Msg[DIAG_W2F_UNEXPECTED_INITV] =
00127 "unexpected INITV kind %d in %s()";
00128 Diag_Msg[DIAG_W2F_UNEXPECTED_DOLOOP_BOUNDOP] =
00129 "unexpected opcode (%s) for DO loop bound in %s()";
00130 Diag_Msg[DIAG_W2F_UNEXPECTED_IMPLIED_DOLOOP] =
00131 "unexpected form of implied do-loop in %s(); Cannot calculate bounds";
00132 Diag_Msg[DIAG_W2F_UNEXPECTED_RETURNSITE] =
00133 "RETURNSITE out of sequence in %s()";
00134 Diag_Msg[DIAG_W2F_UNEXPECTED_CALLSITE] =
00135 "CALLSITE out of sequence in %s()";
00136 Diag_Msg[DIAG_W2F_UNEXPECTED_SUBSTRING_REF] =
00137 "Unexpected (sub)string reference in %s()";
00138 Diag_Msg[DIAG_W2F_UNEXPEXTED_RETURNREG_USE] =
00139 "Unexpected usage of return-registers detected in %s()";
00140 Diag_Msg[DIAG_W2F_UNEXPEXTED_OFFSET] =
00141 "Unexpected offset (%d) for memory location in %s()";
00142 Diag_Msg[DIAG_W2F_UNEXPEXTED_NULL_PTR] =
00143 "Unexpected NULL value for %s in %s()";
00144 Diag_Msg[DIAG_W2F_NONEXISTENT_FLD_PATH] =
00145 "Non-existent path to an FLD of the given object type in %s()";
00146 Diag_Msg[DIAG_W2F_CANNOT_LDA_PREG] =
00147 "Cannot take the address of a pseudo-register";
00148 Diag_Msg[DIAG_W2F_CANNOT_DEREF] =
00149 "Cannot dereference pointer variable in %s(); No pointee specification";
00150 Diag_Msg[DIAG_W2F_UNEXPECTED_NUM_KIDS] =
00151 "Unexpected number of kids (%d), expected %d kids for %s()";
00152 Diag_Msg[DIAG_W2F_UNEXPECTED_CVT] =
00153 "unexpected conversion from %s to %s in %s()";
00154 Diag_Msg[DIAG_W2F_UNEXPECTED_CONTEXT] =
00155 "unexpected context of translation for %s()";
00156
00157 Diag_Msg[DIAG_W2F_UNEXPECTED_TYPE_KIND] =
00158 "unexpected TY_kind (%d) in %s()";
00159 Diag_Msg[DIAG_W2F_UNEXPECTED_TYPE_SIZE] =
00160 "unexpected TY_size (%d) in %s()";
00161 Diag_Msg[DIAG_W2F_UNEXPECTED_BTYPE] =
00162 "unexpected TY_btype (%s) in %s()";
00163 Diag_Msg[DIAG_W2F_EXPECTED_PTR_TO_CHARACTER] =
00164 "expected pointer to character operands in %s()";
00165 Diag_Msg[DIAG_W2F_EXPECTED_PTR] =
00166 "expected pointer TY in %s()";
00167 Diag_Msg[DIAG_W2F_UNEXPECTED_SYMBOL] =
00168 "unexpected form of symbol in %s()";
00169 Diag_Msg[DIAG_W2F_UNEXPECTED_SYMCLASS] =
00170 "unexpected ST_symclass (%d) in %s()";
00171 Diag_Msg[DIAG_W2F_UNEXPECTED_STORECLASS] =
00172 "unexpected ST_sclass (%d) in %s()";
00173 Diag_Msg[DIAG_W2F_UNEXPECTED_SYM_CONST] =
00174 "unexpected symbolic constant in %s()";
00175 Diag_Msg[DIAG_W2F_UNEXPECTED_PRAGMA] =
00176 "unexpected pragma kind in %s()";
00177 Diag_Msg[DIAG_W2F_MISPLACED_PRAGMA] =
00178 "pragma %s will be misplaced in output, and will be emitted as comment";
00179 Diag_Msg[DIAG_W2F_EXPECTED_IDNAME] =
00180 "expected OPC_IDNAME in %s()";
00181 Diag_Msg[DIAG_W2F_INCOMPATIBLE_TYS] =
00182 "incompatible types in %s()";
00183 Diag_Msg[DIAG_W2F_DECLARE_RETURN_PARAM] =
00184 "should not declare return parameter: %s()";
00185 Diag_Msg[DIAG_W2F_BUFFER_ERROR] =
00186 "Error in buffer access: %s";
00187
00188 Diag_Warn_Count = 0;
00189 }
00190
00191
00192 void
00193 Diag_Exit(void)
00194 {
00195
00196 if (Diag_File != NULL) {
00197 fclose (Diag_File);
00198 Diag_File = NULL;
00199 }
00200 }
00201
00202
00203 void
00204 Diag_Set_Phase(const char *phase_name)
00205 {
00206 Set_Error_Phase(phase_name);
00207 (void)strcpy(Diag_Phase_Name, phase_name);
00208 }
00209
00210
00211 void
00212 Diag_Set_File(const char *filename)
00213 {
00214
00215 Set_Error_File(filename);
00216
00217
00218 if (Diag_File != NULL)
00219 {
00220 fclose(Diag_File);
00221 Diag_File = NULL;
00222 }
00223
00224 if (filename != NULL)
00225 {
00226
00227 if (Is_File(filename))
00228 unlink(filename);
00229
00230 Diag_File = fopen(filename, "a");
00231 if (Diag_File == NULL)
00232 fprintf(stderr, "Cannot open error-file: \"%s\"\n", filename);
00233 else if (Same_File(Diag_File, stderr))
00234 {
00235 fclose(Diag_File);
00236 Diag_File = NULL;
00237 fprintf(stderr, "Cannot open stderr as alternate error file\n");
00238 }
00239 }
00240 else
00241 fprintf(stderr,
00242 "Attempt to open name-less file as error file is ignored\n");
00243 }
00244
00245
00246 void
00247 Diag_Set_Max_Diags(int max_allowed_diags)
00248 {
00249 Diag_Max_Diags = max_allowed_diags;
00250 }
00251
00252
00253 int
00254 Diag_Get_Warn_Count(void)
00255 {
00256 return Diag_Warn_Count;
00257 }
00258
00259
00260
00261
00262 void
00263 Diag_Set_Location(const char *file_name, int line_number)
00264 {
00265 Diag_File_Location = file_name;
00266 Diag_Line_Location = line_number;
00267 }
00268
00269
00270 void
00271 Diag_Warning(DIAG_CODE code, ...)
00272 {
00273 char diag_char[512];
00274 va_list arg_ptr;
00275
00276 if (Diag_Max_Diags > Diag_Warn_Count)
00277 {
00278 if (Diag_File_Location != NULL)
00279 (void)sprintf(&diag_char[0],
00280 "%s(%s:%d): WARNING %d: %s\n",
00281 Diag_Phase_Name,
00282 Diag_File_Location, Diag_Line_Location,
00283 code,
00284 Diag_Msg[code]);
00285 else
00286 (void)sprintf(&diag_char[0],
00287 "%s: WARNING %d: %s\n",
00288 Diag_Phase_Name, code, Diag_Msg[code]);
00289
00290 va_start(arg_ptr, code);
00291 vfprintf(stderr, &diag_char[0], arg_ptr);
00292 va_end(arg_ptr);
00293
00294 Diag_Warn_Count++;
00295 }
00296 }
00297
00298
00299 void
00300 Diag_Fatal(DIAG_CODE code, ...)
00301 {
00302 char diag_char[512];
00303 va_list arg_ptr;
00304
00305 if (Diag_File_Location != NULL) {
00306 sprintf(&diag_char[0], "%s(%s:%d): FATAL ERROR: %s\n",
00307 Diag_Phase_Name,
00308 Diag_File_Location, Diag_Line_Location,
00309 Diag_Msg[code]);
00310 } else {
00311 sprintf(&diag_char[0], "%s: FATAL ERROR: %s\n",
00312 Diag_Phase_Name, Diag_Msg[code]);
00313 }
00314
00315 va_start(arg_ptr, code);
00316 vfprintf(stderr, &diag_char[0], arg_ptr);
00317 va_end(arg_ptr);
00318
00319 exit(1);
00320 }
00321