MCPcopy Create free account
hub / github.com/arrayfire/arrayfire / removeComments

Function removeComments

CMakeModules/bin2cpp.cpp:137–253  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

135}
136
137stringstream removeComments(ifstream &input, string &filename) {
138 stringstream ss;
139 char line[256]{
140 '\0'}; // Maximum length of lines in OpenCL code is limited to 256
141 const char *tokenCommentsStart = "/*";
142 const char *tokenCommentsEnd = "*/";
143 const char *tokenCommentsLine = "//";
144 const char *tokenString = "\"";
145 const char *delimitors = " \t;"; // Only the subset we need
146 enum { NO, STRING, ENDOFLINE, MULTILINE } commentsLevel{NO};
147
148 while (input.getline(line, sizeof(line) - 1)) {
149 char local[sizeof(line)];
150 struct segment {
151 char *start;
152 char *end;
153 } del{commentsLevel == MULTILINE ? line : nullptr, nullptr};
154 vector<segment> dels;
155 memcpy(local, line, sizeof(line)); // will be overwritten by strtok
156 local[sizeof(local) - 1] = '\0'; // string is always terminated
157 char *context = nullptr;
158 char *token = STRTOK_CALL(local, delimitors, &context);
159 do {
160 char *subtoken = nullptr;
161 while (token) {
162 switch (commentsLevel) {
163 case MULTILINE:
164 subtoken = strstr(token, tokenCommentsEnd);
165 if (subtoken != nullptr) {
166 if (del.start == nullptr) del.start = line;
167 del.end = subtoken + strlen(tokenCommentsEnd) -
168 local + line;
169 dels.push_back(del);
170 del = {nullptr, nullptr};
171 token = subtoken + strlen(tokenCommentsEnd);
172 commentsLevel = NO;
173 } else {
174 token = nullptr;
175 }
176 break;
177 case STRING:
178 subtoken = strstr(token, tokenString);
179 if (subtoken != nullptr) {
180 token = subtoken + strlen(tokenString);
181 commentsLevel = NO;
182 } else {
183 token = nullptr;
184 }
185 break;
186 case NO: {
187 // select first subtoken inside this token
188 subtoken = strstr(token, tokenCommentsStart);
189 if (subtoken != nullptr) { commentsLevel = MULTILINE; }
190 char *ptr = strstr(token, tokenCommentsLine);
191 if ((ptr != nullptr) &&
192 ((subtoken == nullptr) || (ptr < subtoken))) {
193 commentsLevel = ENDOFLINE;
194 subtoken = ptr;

Callers 1

mainFunction · 0.85

Calls 1

minFunction · 0.50

Tested by

no test coverage detected