~ubuntu-branches/ubuntu/quantal/aspectc++/quantal

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// This file is part of PUMA.
// Copyright (C) 1999-2003  The PUMA developer team.
//                                                                
// This program is free software;  you can redistribute it and/or 
// modify it under the terms of the GNU General Public License as 
// published by the Free Software Foundation; either version 2 of 
// the License, or (at your option) any later version.            
//                                                                
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
// GNU General Public License for more details.                   
//                                                                
// You should have received a copy of the GNU General Public      
// License along with this program; if not, write to the Free     
// Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
// MA  02111-1307  USA                                            

#include "Puma/Unit.h"
#include "Puma/Token.h"
#include "Puma/SysCall.h"
#include "Puma/PreMacro.h"
#include "Puma/PreTree.h"
#include "Puma/TokenStream.h"
#include "Puma/ErrorStream.h"
#include "Puma/PreParserState.h"
#include "Puma/PreMacroManager.h"
#include "Puma/CCommentTokens.h"
#include <time.h>
#include <stdio.h>
#include <string.h>

namespace Puma {


PreParserState::PreParserState () : 
    _Line("__LINE__"), _File("__FILE__"),
    _Date("__DATE__"), _Time("__TIME__"),
    _IncLevel("__INCLUDE_LEVEL__"),
    _Defined("defined"), _Defined1("defined("),
    _LastDay(-1), _LastUnit(0)
 {    
    errors = number = stackPos   = 0;
    noFILE = noLINE = noDATE     = false;
    noTIME = noINCLUDE_LEVEL     = false;
    syntaxError = errorDirective = false;
    syntaxTree                   = (PreTree*) 0;
    sl_token                     = (Token*) 0;
    lastToken = currToken        = (Token*) 0;
    lastType = currType          = Token::ID_UNKNOWN;
    passOnToken                  = true;
    forcedIncludes               = false;
    newline                      = new Token (TOK_WSPACE, "\n");
 }
 

// Check whether the macro name is valid.
bool PreParserState::checkMacroName (PreMacro* macro)
 {
    // If some predefined macros get redefined it has to be remembered.
    if ( macro->getName () == _IncLevel ) {
        noINCLUDE_LEVEL = true;
    } else if ( macro->getName () == _Line ) {
        noLINE = true;
    } else if ( macro->getName () == _Date ) {
        noDATE = true;
    } else if ( macro->getName () == _File ) {
        noFILE = true;
    } else if ( macro->getName () == _Time ) {
        noTIME = true;
        
    // The special name `defined' can't be used for a macro.
    } else if (macro->getName () == _Defined ||
               macro->getName () == _Defined1) {
        *err << macro->location () << sev_error 
             << "`defined' is not a valid macro name" 
             << endMessage; 
        delete macro;
        return false;
    }
    return true;
 }


// Check whether macro is one of the special predefined macros.
PreMacro* PreParserState::checkPredefined (PreMacro* macro, TokenStream* scanner,
                                           PreMacroManager& manager) /*const*/
{
    // Because these macros aren't really predefined, they have to be 
    // prepared => that means to compute their bodies.
    
    // Integer constant representing the depth of nesting in include files.
    if (macro == manager.getIncLevelMacro() && ! noINCLUDE_LEVEL) {
        char value[12];
        sprintf (value, "%li", (number ? number - 2 : scanner->length ()-1));
        macro->setBody(value);
    } 
    
    // String constant representing the current input file.
    else if (macro == manager.getFileMacro() && ! noLINE) {
        if (! number) {
            Unit* unit = scanner->lookup (scanner->length ()-1)->unit ();
            if (unit != _LastUnit) {
                char* buffer = unit->name ();
                char* value  = new char[strlen (buffer) + 3];
                sprintf (value, "\"%s\"", buffer);
                macro->setBody(value);
                delete [] value;
            }
        }
    } 
    
    // Integer constant representing the current input line number.
    else if (macro == manager.getLineMacro() && ! noLINE) {
        char value[12];
        sprintf (value, "%i", currToken->location ().line ());
        macro->setBody(value);
    } 
    
    // String constant representing the current date of the form 
    // "Apr 1 1999".
    else if (macro == manager.getDateMacro() && ! noDATE) {
        time_t curtime = SysCall::time (NULL, err);
        tm* loctime = localtime (&curtime);
        if (_LastDay != loctime->tm_mday) {
            char buffer[16];
            char value[16];
            _LastDay = loctime->tm_mday;
            strftime (buffer, 12, "%b %d %Y", loctime);
            sprintf (value, "\"%s\"", buffer);
            macro->setBody(value);
        }
    } 
    
    // String constant representing the current time of the form 
    // "12:30:45".
    else if (macro == manager.getTimeMacro() && ! noTIME) {
        char buffer[16];
        char value[16];
        time_t curtime = SysCall::time (NULL, err);
        tm* loctime = localtime (&curtime);
        strftime (buffer, 9, "%H:%M:%S", loctime);
        sprintf (value, "\"%s\"", buffer);
        macro->setBody(value);
    }
    
    return macro;
 }


// Check whether macro is a "self-referential" macro.
bool PreParserState::checkToExpand (Token *token, PreMacro* macro,
                                    TokenStream* scanner, 
                                    Array<PreMacro*>& macroStack,
                                    set<Token*> &prescanned)
 {
    // A special feature of ANSI Standard C is that the self-reference
    // is not considered a macro call, it is passed into the preprocessor
    // output unchanged.
    if (number) {
        stackPos = scanner->length () - number;
        
        // Search macro on stack. If found it is a self-referential macro.
        for (int i = 0; i <= stackPos; i++)
            if (strcmp (macro->getName (), macroStack[i]->getName ()) == 0)
                return false;
    }

    // Macros names found in prescaned macro arguments can only be self-
    // referential macros. They shall not be expanded again
    if (prescanned.find (token) != prescanned.end ()) {
      return false;
    }
    
    return true;
 }


} // namespace Puma