* Parse the last line of @p text and extract information about any existing include path from it. */
| 27 | * Parse the last line of @p text and extract information about any existing include path from it. |
| 28 | */ |
| 29 | IncludePathProperties IncludePathProperties::parseText(const QString& text, int rightBoundary) |
| 30 | { |
| 31 | IncludePathProperties properties; |
| 32 | |
| 33 | int idx = text.lastIndexOf(QLatin1Char('\n')); |
| 34 | if (idx == -1) { |
| 35 | idx = 0; |
| 36 | } |
| 37 | if (rightBoundary == -1) { |
| 38 | rightBoundary = text.length(); |
| 39 | } |
| 40 | |
| 41 | // what follows is a relatively simple parser for include lines that may contain comments, i.e.: |
| 42 | // /*comment*/ #include /*comment*/ "path.h" /*comment*/ |
| 43 | enum FindState { |
| 44 | FindBang, |
| 45 | FindInclude, |
| 46 | FindType, |
| 47 | FindTypeEnd |
| 48 | }; |
| 49 | FindState state = FindBang; |
| 50 | QChar expectedEnd = QLatin1Char('>'); |
| 51 | for (; idx < text.size(); ++idx) { |
| 52 | const auto c = text.at(idx); |
| 53 | if (c.isSpace()) { |
| 54 | continue; |
| 55 | } |
| 56 | if (c == QLatin1Char('/') && state != FindTypeEnd) { |
| 57 | // skip comments |
| 58 | if (idx >= text.length() - 1 || text.at(idx + 1) != QLatin1Char('*')) { |
| 59 | properties.valid = false; |
| 60 | return properties; |
| 61 | } |
| 62 | idx += 2; |
| 63 | while (idx < text.length() - 1 && (text.at(idx) != QLatin1Char('*') || text.at(idx + 1) != QLatin1Char('/'))) { |
| 64 | ++idx; |
| 65 | } |
| 66 | if (idx >= text.length() - 1 || text.at(idx) != QLatin1Char('*') || text.at(idx + 1) != QLatin1Char('/')) { |
| 67 | properties.valid = false; |
| 68 | return properties; |
| 69 | } |
| 70 | ++idx; |
| 71 | continue; |
| 72 | } |
| 73 | switch (state) { |
| 74 | case FindBang: |
| 75 | if (c != QLatin1Char('#')) { |
| 76 | return properties; |
| 77 | } |
| 78 | state = FindInclude; |
| 79 | break; |
| 80 | case FindInclude: { |
| 81 | constexpr QLatin1String includeString("include", 7); |
| 82 | if (!matchesAtOffset(text, idx, includeString)) { |
| 83 | return properties; |
| 84 | } |
| 85 | idx += includeString.size() - 1; |
| 86 | state = FindType; |
nothing calls this directly
no test coverage detected