-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreprocess_c.cpp
64 lines (56 loc) · 2.43 KB
/
preprocess_c.cpp
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
/*
* Copyright (c) 2018-2020, High Performance Computing Architecture and System
* research laboratory at University of North Carolina at Charlotte (HPCAS@UNCC)
* and Lawrence Livermore National Security, LLC.
*
* SPDX-License-Identifier: (BSD-3-Clause)
*/
#include <iostream>
#include <regex>
#include <fstream>
std::vector<std::string>* preProcessC(std::ifstream&);
std::vector<std::string>* preProcessC(std::ifstream& input_file) {
std::string input_pragma;
int total_amount = 0;
int line_no = 0;
int current_pragma_line_no = 1;
std::vector<std::string> *omp_pragmas = new std::vector<std::string>();
char current_char = input_file.peek();
std::string current_line;
std::regex c_regex ("^([[:blank:]]*#pragma)([[:blank:]]+)(omp)[[:blank:]]+(.*)");
std::regex comment_regex ("[/][*]([^*]|[*][^/])*[*][/]");
std::regex continue_regex ("([\\\\]+[[:blank:]]*$)");
while (!input_file.eof()) {
line_no += 1;
switch (current_char) {
case '\n':
input_file.seekg(1, std::ios_base::cur);
break;
default:
std::getline(input_file, current_line);
// remove the inline comments
current_line = std::regex_replace(current_line, comment_regex, "");
input_pragma = "";
if (std::regex_match(current_line, c_regex)) {
// combine continuous lines if necessary
while (std::regex_search(current_line, continue_regex)) {
// remove the slash part at the end
current_line = std::regex_replace(current_line, continue_regex, "");
// add the current line to the pragma string
input_pragma += current_line;
// get the next line
std::getline(input_file, current_line);
// remove the inline comments of next line
current_line = std::regex_replace(current_line, comment_regex, "");
};
input_pragma += current_line;
total_amount += 1;
current_pragma_line_no = line_no;
//std::cout << input_pragma << std::endl;
omp_pragmas->push_back(input_pragma);
}
};
current_char = input_file.peek();
};
return omp_pragmas;
}