1 /*****************************************************************************/
5 /* Assertions for the ld65 linker */
9 /* (C) 2003-2008, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
37 #include "assertdefs.h"
51 /*****************************************************************************/
53 /*****************************************************************************/
57 /* List with all assertions */
58 static Collection Assertions = STATIC_COLLECTION_INITIALIZER;
62 /*****************************************************************************/
64 /*****************************************************************************/
68 Assertion* ReadAssertion (FILE* F, struct ObjData* O)
69 /* Read an assertion from the given file */
72 Assertion* A = xmalloc (sizeof (Assertion));
74 /* Read the fields from the file */
75 A->Expr = ReadExpr (F, O);
76 A->Action = ReadVar (F);
77 A->Msg = MakeGlobalStringId (O, ReadVar (F));
78 ReadFilePos (F, &A->Pos);
80 /* Set remaining fields */
83 /* Add the assertion to the global list */
84 CollAppend (&Assertions, A);
86 /* Return the new struct */
92 void CheckAssertions (void)
93 /* Check all assertions */
97 /* Walk over all assertions */
98 for (I = 0; I < CollCount (&Assertions); ++I) {
100 /* Get the assertion */
101 Assertion* A = CollAtUnchecked (&Assertions, I);
103 /* If the expression is not constant, we're not able to handle it */
104 if (!IsConstExpr (A->Expr)) {
105 Warning ("Cannot evaluate assertion in module `%s', line %lu",
106 GetSourceFileName (A->Obj, A->Pos.Name), A->Pos.Line);
107 } else if (GetExprVal (A->Expr) == 0) {
109 /* Assertion failed */
110 const char* Module = GetSourceFileName (A->Obj, A->Pos.Name);
111 const char* Message = GetString (A->Msg);
115 case ASSERT_ACT_WARN:
116 Warning ("%s(%lu): %s", Module, A->Pos.Line, Message);
119 case ASSERT_ACT_ERROR:
120 Error ("%s(%lu): %s", Module, A->Pos.Line, Message);
124 Internal ("Invalid assertion action (%u) in module `%s', "
125 "line %lu (file corrupt?)",
126 A->Action, Module, A->Pos.Line);