| 1 | //===- unittest/Format/FormatTestUtils.h - Formatting unit tests ----------===// |
|---|---|
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines utility functions for Clang-Format related tests. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef LLVM_CLANG_UNITTESTS_FORMAT_FORMATTESTUTILS_H |
| 14 | #define LLVM_CLANG_UNITTESTS_FORMAT_FORMATTESTUTILS_H |
| 15 | |
| 16 | #include "clang/Format/Format.h" |
| 17 | #include "llvm/ADT/StringRef.h" |
| 18 | |
| 19 | namespace clang { |
| 20 | namespace format { |
| 21 | namespace test { |
| 22 | |
| 23 | inline FormatStyle getGoogleStyle() { |
| 24 | return getGoogleStyle(Language: FormatStyle::LK_Cpp); |
| 25 | } |
| 26 | |
| 27 | // When HandleHash is false, preprocessor directives starting with hash will not |
| 28 | // be on separate lines. This is needed because Verilog uses hash for other |
| 29 | // purposes. |
| 30 | inline std::string messUp(StringRef Code, bool HandleHash = true) { |
| 31 | std::string MessedUp(Code.str()); |
| 32 | bool InComment = false; |
| 33 | bool InPreprocessorDirective = false; |
| 34 | bool JustReplacedNewline = false; |
| 35 | for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) { |
| 36 | if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') { |
| 37 | if (JustReplacedNewline) |
| 38 | MessedUp[i - 1] = '\n'; |
| 39 | InComment = true; |
| 40 | } else if (HandleHash && MessedUp[i] == '#' && |
| 41 | (JustReplacedNewline || i == 0 || MessedUp[i - 1] == '\n')) { |
| 42 | if (i != 0) |
| 43 | MessedUp[i - 1] = '\n'; |
| 44 | InPreprocessorDirective = true; |
| 45 | } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') { |
| 46 | MessedUp[i] = ' '; |
| 47 | MessedUp[i + 1] = ' '; |
| 48 | } else if (MessedUp[i] == '\n') { |
| 49 | if (InComment) { |
| 50 | InComment = false; |
| 51 | } else if (InPreprocessorDirective) { |
| 52 | InPreprocessorDirective = false; |
| 53 | } else { |
| 54 | JustReplacedNewline = true; |
| 55 | MessedUp[i] = ' '; |
| 56 | } |
| 57 | } else if (MessedUp[i] != ' ') { |
| 58 | JustReplacedNewline = false; |
| 59 | } |
| 60 | } |
| 61 | std::string WithoutWhitespace; |
| 62 | if (MessedUp[0] != ' ') |
| 63 | WithoutWhitespace.push_back(c: MessedUp[0]); |
| 64 | for (unsigned i = 1, e = MessedUp.size(); i != e; ++i) |
| 65 | if (MessedUp[i] != ' ' || MessedUp[i - 1] != ' ') |
| 66 | WithoutWhitespace.push_back(c: MessedUp[i]); |
| 67 | return WithoutWhitespace; |
| 68 | } |
| 69 | |
| 70 | } // end namespace test |
| 71 | } // end namespace format |
| 72 | } // end namespace clang |
| 73 | |
| 74 | #endif |
| 75 |
