1 /*****************************************************************************/
5 /* Assertions for the ld65 linker */
9 /* (C) 2003-2011, 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"
52 /*****************************************************************************/
54 /*****************************************************************************/
58 /* Assertion struct decl */
60 Collection LineInfos; /* File position of assertion */
61 ExprNode* Expr; /* Expression to evaluate */
62 AssertAction Action; /* What to do */
63 unsigned Msg; /* Message to print */
64 ObjData* Obj; /* Object file containing the assertion */
67 /* List with all assertions */
68 static Collection Assertions = STATIC_COLLECTION_INITIALIZER;
72 /*****************************************************************************/
74 /*****************************************************************************/
78 Assertion* ReadAssertion (FILE* F, struct ObjData* O)
79 /* Read an assertion from the given file */
82 Assertion* A = xmalloc (sizeof (Assertion));
84 /* Read the fields from the file */
85 A->LineInfos = EmptyCollection;
86 A->Expr = ReadExpr (F, O);
87 A->Action = (AssertAction) ReadVar (F);
88 A->Msg = MakeGlobalStringId (O, ReadVar (F));
89 ReadLineInfoList (F, O, &A->LineInfos);
91 /* Set remaining fields */
94 /* Add the assertion to the global list */
95 CollAppend (&Assertions, A);
97 /* Return the new struct */
103 void CheckAssertions (void)
104 /* Check all assertions */
108 /* Walk over all assertions */
109 for (I = 0; I < CollCount (&Assertions); ++I) {
115 /* Get the assertion */
116 Assertion* A = CollAtUnchecked (&Assertions, I);
118 /* Ignore assertions that shouldn't be handled at link time */
119 if (!AssertAtLinkTime (A->Action)) {
123 /* Retrieve the relevant line info for this assertion */
124 LI = CollConstAt (&A->LineInfos, 0);
126 /* Get file name and line number from the source */
127 Module = GetSourceName (LI);
128 Line = GetSourceLine (LI);
130 /* If the expression is not constant, we're not able to handle it */
131 if (!IsConstExpr (A->Expr)) {
132 Warning ("Cannot evaluate assertion in module `%s', line %u",
134 } else if (GetExprVal (A->Expr) == 0) {
136 /* Assertion failed */
137 const char* Message = GetString (A->Msg);
141 case ASSERT_ACT_WARN:
142 case ASSERT_ACT_LDWARN:
143 Warning ("%s(%u): %s", Module, Line, Message);
146 case ASSERT_ACT_ERROR:
147 case ASSERT_ACT_LDERROR:
148 Error ("%s(%u): %s", Module, Line, Message);
152 Internal ("Invalid assertion action (%u) in module `%s', "
153 "line %u (file corrupt?)",
154 A->Action, Module, Line);