]> git.sur5r.net Git - cc65/blob - src/ld65/asserts.c
Better handling of imports in the ExprNode structure.
[cc65] / src / ld65 / asserts.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 asserts.c                                 */
4 /*                                                                           */
5 /*                      Assertions for the ld65 linker                       */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2003-2009, Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 52                                           */
11 /*                D-70794 Filderstadt                                        */
12 /* EMail:         uz@cc65.org                                                */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 /* common */
37 #include "assertion.h"
38 #include "coll.h"
39 #include "xmalloc.h"
40
41 /* ld65 */
42 #include "asserts.h"
43 #include "error.h"
44 #include "expr.h"
45 #include "fileio.h"
46 #include "objdata.h"
47 #include "spool.h"
48
49
50
51 /*****************************************************************************/
52 /*                                   Data                                    */
53 /*****************************************************************************/
54
55
56
57 /* Assertion struct decl */
58 struct Assertion {
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 */
64 };
65
66 /* List with all assertions */
67 static Collection Assertions = STATIC_COLLECTION_INITIALIZER;
68
69
70
71 /*****************************************************************************/
72 /*                                   Code                                    */
73 /*****************************************************************************/
74
75
76
77 Assertion* ReadAssertion (FILE* F, struct ObjData* O)
78 /* Read an assertion from the given file */
79 {
80     /* Allocate memory */
81     Assertion* A = xmalloc (sizeof (Assertion));
82
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);
88
89     /* Set remaining fields */
90     A->Obj = O;
91
92     /* Add the assertion to the global list */
93     CollAppend (&Assertions, A);
94
95     /* Return the new struct */
96     return A;
97 }
98
99
100
101 void CheckAssertions (void)
102 /* Check all assertions */
103 {
104     unsigned I;
105
106     /* Walk over all assertions */
107     for (I = 0; I < CollCount (&Assertions); ++I) {
108
109         /* Get the assertion */
110         Assertion* A = CollAtUnchecked (&Assertions, I);
111
112         /* Ignore assertions that shouldn't be handled at link time */
113         if (!AssertAtLinkTime (A->Action)) {
114             continue;
115         }
116
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) {
122
123             /* Assertion failed */
124             const char* Module  = GetSourceFileName (A->Obj, A->Pos.Name);
125             const char* Message = GetString (A->Msg);
126
127             switch (A->Action) {
128
129                 case ASSERT_ACT_WARN:
130                 case ASSERT_ACT_LDWARN:
131                     Warning ("%s(%lu): %s", Module, A->Pos.Line, Message);
132                     break;
133
134                 case ASSERT_ACT_ERROR:
135                 case ASSERT_ACT_LDERROR:
136                     Error ("%s(%lu): %s", Module, A->Pos.Line, Message);
137                     break;
138
139                 default:
140                     Internal ("Invalid assertion action (%u) in module `%s', "
141                               "line %lu (file corrupt?)",
142                               A->Action, Module, A->Pos.Line);
143                     break;
144             }
145         }
146     }
147 }
148
149
150