1 /*****************************************************************************/
5 /* Assertions for the ld65 linker */
9 /* (C) 2003-2009, 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 "assertion.h"
51 /*****************************************************************************/
53 /*****************************************************************************/
57 /* Assertion struct decl */
59 FilePos Pos; /* File position of assertion */
60 ExprNode* Expr; /* Expression to evaluate */
61 AssertAction Action; /* What to do */
62 unsigned Msg; /* Message to print */
63 ObjData* Obj; /* Object file containing the assertion */
66 /* List with all assertions */
67 static Collection Assertions = STATIC_COLLECTION_INITIALIZER;
71 /*****************************************************************************/
73 /*****************************************************************************/
77 Assertion* ReadAssertion (FILE* F, struct ObjData* O)
78 /* Read an assertion from the given file */
81 Assertion* A = xmalloc (sizeof (Assertion));
83 /* Read the fields from the file */
84 A->Expr = ReadExpr (F, O);
85 A->Action = (AssertAction) ReadVar (F);
86 A->Msg = MakeGlobalStringId (O, ReadVar (F));
87 ReadFilePos (F, &A->Pos);
89 /* Set remaining fields */
92 /* Add the assertion to the global list */
93 CollAppend (&Assertions, A);
95 /* Return the new struct */
101 void CheckAssertions (void)
102 /* Check all assertions */
106 /* Walk over all assertions */
107 for (I = 0; I < CollCount (&Assertions); ++I) {
109 /* Get the assertion */
110 Assertion* A = CollAtUnchecked (&Assertions, I);
112 /* Ignore assertions that shouldn't be handled at link time */
113 if (!AssertAtLinkTime (A->Action)) {
117 /* If the expression is not constant, we're not able to handle it */
118 if (!IsConstExpr (A->Expr)) {
119 Warning ("Cannot evaluate assertion in module `%s', line %lu",
120 GetSourceFileName (A->Obj, A->Pos.Name), A->Pos.Line);
121 } else if (GetExprVal (A->Expr) == 0) {
123 /* Assertion failed */
124 const char* Module = GetSourceFileName (A->Obj, A->Pos.Name);
125 const char* Message = GetString (A->Msg);
129 case ASSERT_ACT_WARN:
130 case ASSERT_ACT_LDWARN:
131 Warning ("%s(%lu): %s", Module, A->Pos.Line, Message);
134 case ASSERT_ACT_ERROR:
135 case ASSERT_ACT_LDERROR:
136 Error ("%s(%lu): %s", Module, A->Pos.Line, Message);
140 Internal ("Invalid assertion action (%u) in module `%s', "
141 "line %lu (file corrupt?)",
142 A->Action, Module, A->Pos.Line);