[gn build] Port 0e80f9a1b51e
[llvm-project.git] / clang / unittests / Format / FormatTestUtils.h
blobcb55b2d747b628f6f73c0a33ef46a1d9badcaf93
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.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_UNITTESTS_FORMAT_FORMATTESTUTILS_H
14 #define LLVM_CLANG_UNITTESTS_FORMAT_FORMATTESTUTILS_H
16 #include "clang/Format/Format.h"
17 #include "llvm/ADT/StringRef.h"
19 namespace clang {
20 namespace format {
21 namespace test {
23 inline FormatStyle getGoogleStyle() {
24 return getGoogleStyle(FormatStyle::LK_Cpp);
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] = ' ';
57 } else if (MessedUp[i] != ' ') {
58 JustReplacedNewline = false;
61 std::string WithoutWhitespace;
62 if (MessedUp[0] != ' ')
63 WithoutWhitespace.push_back(MessedUp[0]);
64 for (unsigned i = 1, e = MessedUp.size(); i != e; ++i)
65 if (MessedUp[i] != ' ' || MessedUp[i - 1] != ' ')
66 WithoutWhitespace.push_back(MessedUp[i]);
67 return WithoutWhitespace;
70 } // end namespace test
71 } // end namespace format
72 } // end namespace clang
74 #endif