1 #include "xi-Template.h"
6 void XStr::append(const char* _s) {
9 while (len >= blklen) {
20 void XStr::append(char c) {
27 void XStr::initTo(const char* _s) {
30 while (len >= blklen) {
37 XStr::XStr() { initTo(""); }
38 XStr::XStr(const char* _s) { initTo(_s); }
39 XStr::XStr(const XStr& _s) { initTo(_s.get_string_const()); }
46 XStr& XStr::operator<<(int i) {
48 sprintf(tmp, "%d", i);
53 void XStr::line_append(const char c) {
55 for (unsigned int i = 0; i < len; i++) {
62 initTo(xs.charstar());
65 void XStr::line_append_padding(const char c, int lineWidth) {
69 for (unsigned int i = 0; i < len; i++) {
72 while (count++ < lineWidth - 1) xs << " ";
75 } else if (s[i] == '\t') {
76 // found tab, convert to 2 spaces
80 // found non-line ending
86 initTo(xs.charstar());
89 void XStr::spew(const char* b, const char* a1, const char* a2, const char* a3,
90 const char* a4, const char* a5) {
92 int i, length = strlen(b);
93 for (i = 0; i < length; i++) {
97 cout << "Internal Error\n";
104 cout << "Internal Error\n";
111 cout << "Internal Error\n";
118 cout << "Internal Error\n";
125 cout << "Internal Error\n";
136 void XStr::replace(const char a, const char b) {
137 for (unsigned int i = 0; i < len; i++) {
138 if (s[i] == a) s[i] = b;
142 extern const char* cur_file;
144 // Fatal error function
145 void die(const char* why, int line) {
147 fprintf(stderr, "%s: Charmxi fatal error> %s\n", cur_file, why);
149 fprintf(stderr, "%s:%d: Charmxi fatal error> %s\n", cur_file, line, why);
153 char* fortranify(const char* s, const char* suff1, const char* suff2, const char* suff3) {
154 int i, len1 = strlen(s), len2 = strlen(suff1), len3 = strlen(suff2),
155 len4 = strlen(suff3);
156 int c = len1 + len2 + len3 + len4;
157 char str[1024], strUpper[1024];
162 for (i = 0; i < c + 1; i++) str[i] = tolower(str[i]);
163 for (i = 0; i < c + 1; i++) strUpper[i] = toupper(str[i]);
165 retVal = new char[2 * c + 20];
166 strcpy(retVal, "FTN_NAME(");
167 strcat(retVal, strUpper);
175 XStr generateTemplateSpec(TVarList* tspec, bool printDefault) {
180 tspec->genLong(str, printDefault);
187 const char* forWhomStr(forWhom w) {
190 return Prefix::Proxy;
192 return Prefix::ProxyElement;
194 return Prefix::ProxySection;
196 return Prefix::Index;
204 // Make the name lower case
205 void templateGuardBegin(bool templateOnly, XStr& str) {
208 << "CK_TEMPLATES_ONLY\n";
211 << "CK_TEMPLATES_ONLY\n";
213 void templateGuardEnd(XStr& str) { str << "#endif /* CK_TEMPLATES_ONLY */\n"; }
215 // This replaces the line containing a single '#' token with a '#line'
216 // directive, as described in SerialConstruct::generateCode.
217 std::string addLineNumbers(char* str, const char* filename) {
220 for (int i = 0; i < s.length(); ++i) {
226 if (i > 0 && s[i - 1] == '\n' && s[i + 1] == '\n') {
227 std::stringstream ss;
228 ss << "#line " << lineNo + 1 << " \"" << filename << "\"";
229 s.replace(s.begin() + i, s.begin() + i + 1, ss.str());
236 // The following three functions are a workaround for bug #734.
237 void sanitizeRange(std::string& code, int i, int j) {
238 for (int k = i; k <= j; ++k) {
273 void desanitizeCode(std::string& code) {
274 for (int i = 0; i < code.size(); ++i) {
309 void sanitizeComments(std::string& code) {
312 if(code.length() == 0)
315 for (i = 0; i < code.length() - 1; ++i) {
316 if (code[i] == '/') {
318 switch (code[i + 1]) {
322 for (; !(code[i] == '*' && code[i + 1] == '/'); ++i)
328 while (code[++i] != '\n')
333 // Case 3: not a comment
338 sanitizeRange(code, h, i - 1);
344 void sanitizeStrings(std::string& code) {
346 bool in_string = false;
347 for (i = 0; i < code.size(); ++i) {
348 if (code[i] == '\\') {
349 // The next character cannot possibly end a string since this '\'
350 // would escape it; just skip over it.
352 } else if (code[i] == '"') {
354 sanitizeRange(code, h, i - 1);
364 // charmxi error printing methods
365 std::string _get_caret_line(int err_line_start, int first_col, int last_col) {
366 std::string caret_line(first_col - err_line_start - 1, ' ');
367 caret_line += std::string(last_col - first_col + 1, '^');
372 void _pretty_header(std::string type, std::string msg, int first_col, int last_col,
373 int first_line, int last_line) {
374 std::cerr << cur_file << ":" << first_line << ":";
376 if (first_col != -1) std::cerr << first_col << "-" << last_col << ": ";
378 std::cerr << type << ": " << msg << std::endl;
381 void _pretty_print(std::string type, std::string msg, int first_col, int last_col,
382 int first_line, int last_line) {
383 _pretty_header(type, msg, first_col, last_col, first_line, last_line);
385 if (first_line <= inputBuffer.size() && first_line <= last_line &&
386 first_col <= last_col) {
387 std::string err_line = inputBuffer[first_line - 1];
389 if (err_line.length() != 0) {
390 int err_line_start = err_line.find_first_not_of(" \t\r\n");
391 err_line.erase(0, err_line_start);
393 std::string caret_line;
395 caret_line = _get_caret_line(err_line_start, first_col, last_col);
397 std::cerr << " " << err_line << std::endl;
399 if (first_col != -1) std::cerr << " " << caret_line;
400 std::cerr << std::endl;
405 void pretty_msg(std::string type, std::string msg, int first_col, int last_col,
406 int first_line, int last_line) {
407 if (first_line == -1) first_line = lineno;
408 if (last_line == -1) last_line = lineno;
409 _pretty_print(type, msg, first_col, last_col, first_line, last_line);
412 void pretty_msg_noline(std::string type, std::string msg, int first_col, int last_col,
413 int first_line, int last_line) {
414 if (first_line == -1) first_line = lineno;
415 if (last_line == -1) last_line = lineno;
416 _pretty_header(type, msg, first_col, last_col, first_line, last_line);
423 const char* Proxy = "CProxy_";
424 const char* ProxyElement = "CProxyElement_";
425 const char* ProxySection = "CProxySection_";
426 const char* Message = "CMessage_";
427 const char* Index = "CkIndex_";
428 const char* Python = "CkPython_";
430 } // namespace Prefix