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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
/*************************************************************************/
/* OPARI Version 1.1 */
/* Copyright (C) 2001 */
/* Forschungszentrum Juelich, Zentralinstitut fuer Angewandte Mathematik */
/*************************************************************************/
#include <iostream>
using std::cerr;
#include <stack>
using std::stack;
#include <vector>
using std::vector;
#include <map>
using std::map;
#include <string>
using std::getline;
using std::string;
#include "opari.h"
#include "handler.h"
namespace {
string find_next_word(vector<string>& preStmt, unsigned size,
unsigned& pline, string::size_type& ppos) {
while ( pline < size ) {
string::size_type wbeg = preStmt[pline].find_first_not_of(" \t", ppos);
if ( preStmt[pline][wbeg] == '\\' || wbeg == string::npos ) {
++pline;
if ( pline < size ) { ppos = 0; } else { return ""; }
} else {
ppos = preStmt[pline].find_first_of(" \t()", wbeg);
return preStmt[pline].substr(wbeg,
ppos==string::npos ? ppos : ppos-wbeg);
}
}
return "";
}
bool process_preStmt(vector<string>& preStmt, ostream& os,
const char* infile, int lineno,
string::size_type ppos, bool* e, bool* f) {
unsigned s = preStmt.size();
bool inComment = false;
// "remove" comments
for (unsigned i=0; i<s; ++i) {
string::size_type pos = 0;
string& line = preStmt[i];
while ( pos < line.size() ) {
if ( inComment ) {
// look for comment end
if ( line[pos] == '*' && line[pos+1] == '/' ) {
line[pos++] = ' ';
inComment = false;
}
line[pos++] = ' ';
} else if ( line[pos] == '/' ) {
pos++;
if ( line[pos] == '/' ) {
// c++ comments
line[pos-1] = ' ';
line[pos++] = ' ';
while ( pos < line.size() ) { line[pos++] = ' '; }
} else if ( line[pos] == '*' ) {
// c comment start
line[pos-1] = ' ';
line[pos++] = ' ';
inComment = true;
}
} else {
pos++;
}
}
}
// OpenMP pragma?
unsigned pline = 0;
if ( find_next_word(preStmt, s, pline, ppos) == "pragma" ) {
string word = find_next_word(preStmt, s, pline, ppos);
if ( (word == "omp") || (word == "pomp") ) {
OMPragmaC* p = new OMPragmaC(infile, lineno, pline, ppos, preStmt);
process_pragma(p, os, e, f);
return true;
}
}
for (unsigned i=0; i<s; ++i) os << preStmt[i] << "\n";
preStmt.clear();
return false;
}
}
void process_c_or_cxx(istream& is, const char* infile, const char* incfile,
ostream& os) {
string line;
bool inComment = false;
bool preContLine = false;
bool requiresEnd = true;
bool isFor = false;
int lineno = 1;
string::size_type pos = 0;
int level = 0;
int numSemi = 0;
string::size_type lstart = string::npos;
vector<string> preStmt;
vector<string> endStmt;
stack<int> nextEnd;
map<string,string> wrapper;
wrapper["omp_init_lock"] = "pomp_init_lock";
wrapper["omp_destroy_lock"] = "pomp_destroy_lock";
wrapper["omp_set_lock"] = "pomp_set_lock";
wrapper["omp_unset_lock"] = "pomp_unset_lock";
wrapper["omp_test_lock"] = "pomp_test_lock";
wrapper["omp_init_nest_lock"] = "pomp_init_nest_lock";
wrapper["omp_destroy_nest_lock"] = "pomp_destroy_nest_lock";
wrapper["omp_set_nest_lock"] = "pomp_set_nest_lock";
wrapper["omp_unset_nest_lock"] = "pomp_unset_nest_lock";
wrapper["omp_test_nest_lock"] = "pomp_test_nest_lock";
nextEnd.push(-1);
while ( getline(is, line) ) {
if ( preContLine ) {
/*
* preprocessor directive continuation
*/
preStmt.push_back(line);
if ( line[line.size()-1] != '\\' ) {
preContLine = false;
if ( process_preStmt(preStmt, os, infile, lineno-preStmt.size()+1,
lstart+1, &requiresEnd, &isFor) ) {
if ( requiresEnd ) {
nextEnd.push(level);
numSemi = isFor ? 3 : 1;
} else {
numSemi = 0;
}
}
}
} else if ( !inComment &&
((lstart = line.find_first_not_of(" \t")) != string::npos) &&
line[lstart] == '#' ) {
/*
* preprocessor directive
*/
preStmt.push_back(line);
if ( line[line.size()-1] == '\\' ) {
preContLine = true;
} else {
if ( process_preStmt(preStmt, os, infile, lineno, lstart+1,
&requiresEnd, &isFor) ) {
if ( requiresEnd ) {
nextEnd.push(level);
numSemi = isFor ? 3 : 1;
} else {
numSemi = 0;
}
}
}
} else {
/*
* regular line
*/
bool newlinePrinted = false;
while ( pos < line.size() ) {
newlinePrinted = false;
if ( inComment ) {
// look for comment end
if ( line[pos] == '*' && line[pos+1] == '/' ) {
os << "*/";
inComment = false;
pos += 2;
} else {
os << line[pos++];
}
} else if ( line[pos] == '/' ) {
pos++;
if ( line[pos] == '/' ) {
// c++ comments
pos++;
os << "//";
while ( pos < line.size() ) { os << line[pos++]; }
} else if ( line[pos] == '*' ) {
// c comment start
pos++;
os << "/*";
inComment = true;
} else {
os << '/';
}
} else if ( line[pos] == '\"' ) {
// character string constant
os << "\"";
do {
pos++;
if ( line[pos] == '\\' ) {
os << '\\';
pos++;
if ( line[pos] == '\"' ) {
os << '\"';
pos++;
}
}
os << line[pos];
}
while ( line[pos] != '\"' );
pos++;
} else if ( line[pos] == '\'' ) {
// character constant
os << "\'";
do {
pos++;
if ( line[pos] == '\\' ) {
os << '\\';
pos++;
if ( line[pos] == '\'' ) {
os << '\'';
pos++;
}
}
os << line[pos];
}
while ( line[pos] != '\'' );
pos++;
} else if ( isalpha(line[pos]) || line[pos] == '_' ) {
// identifier
string::size_type startpos = pos;
while ( pos < line.size() &&
(isalnum(line[pos]) || line[pos]=='_') ) {
pos++;
}
string ident(line, startpos, pos-startpos);
map<string,string>::iterator w = wrapper.find(ident);
if ( w != wrapper.end() && instrument_locks() )
os << w->second;
else
os << ident;
} else if ( line[pos] == '{' ) {
// block open
os << line[pos++];
level++;
numSemi = 0;
} else if ( line[pos] == '}' ) {
// block close
os << line[pos++];
level--;
if ( nextEnd.top() == level ) {
int moreChars = (pos < line.size());
os << '\n';
newlinePrinted = true;
// while because block can actually close more than one pragma
while ( nextEnd.top() == level ) {
// hack: use pline (arg3) for correction value for line info
process_pragma(new OMPragmaC(infile, lineno+1-moreChars,
1-moreChars, 0, endStmt), os);
nextEnd.pop();
}
if ( moreChars ) for(unsigned i=0; i<pos; ++i) os << ' ';
}
} else if ( line[pos] == ';' ) {
// statement end
os << line[pos++];
numSemi--;
if ( numSemi == 0 ) {
int moreChars = (pos < line.size());
os << '\n';
newlinePrinted = true;
// hack: use pline (arg3) for correction value for line info
process_pragma(new OMPragmaC(infile, lineno+1-moreChars,
1-moreChars, 0, endStmt), os);
nextEnd.pop();
// check whether statement actually closes more pragma
while ( nextEnd.top() == level ) {
// hack: use pline (arg3) for correction value for line info
process_pragma(new OMPragmaC(infile, lineno+1-moreChars,
1-moreChars, 0, endStmt), os);
nextEnd.pop();
}
if ( moreChars ) for(unsigned i=0; i<pos; ++i) os << ' ';
}
} else {
os << line[pos++];
}
}
if ( !newlinePrinted ) os << '\n';
}
++lineno;
pos = 0;
}
// check end position stack
if ( nextEnd.top() != -1 ) {
cerr << "ERROR: unbalanced pragma nesting\n";
cleanup_and_exit();
}
}
|