FormatTestUtils.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //===- unittest/Format/FormatTestUtils.h - Formatting unit tests ----------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines utility functions for Clang-Format related tests.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CLANG_UNITTESTS_FORMAT_FORMATTESTUTILS_H
  14. #define LLVM_CLANG_UNITTESTS_FORMAT_FORMATTESTUTILS_H
  15. #include "llvm/ADT/StringRef.h"
  16. namespace clang {
  17. namespace format {
  18. namespace test {
  19. inline std::string messUp(llvm::StringRef Code) {
  20. std::string MessedUp(Code.str());
  21. bool InComment = false;
  22. bool InPreprocessorDirective = false;
  23. bool JustReplacedNewline = false;
  24. for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) {
  25. if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') {
  26. if (JustReplacedNewline)
  27. MessedUp[i - 1] = '\n';
  28. InComment = true;
  29. } else if (MessedUp[i] == '#' && (JustReplacedNewline || i == 0)) {
  30. if (i != 0)
  31. MessedUp[i - 1] = '\n';
  32. InPreprocessorDirective = true;
  33. } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') {
  34. MessedUp[i] = ' ';
  35. MessedUp[i + 1] = ' ';
  36. } else if (MessedUp[i] == '\n') {
  37. if (InComment) {
  38. InComment = false;
  39. } else if (InPreprocessorDirective) {
  40. InPreprocessorDirective = false;
  41. } else {
  42. JustReplacedNewline = true;
  43. MessedUp[i] = ' ';
  44. }
  45. } else if (MessedUp[i] != ' ') {
  46. JustReplacedNewline = false;
  47. }
  48. }
  49. std::string WithoutWhitespace;
  50. if (MessedUp[0] != ' ')
  51. WithoutWhitespace.push_back(MessedUp[0]);
  52. for (unsigned i = 1, e = MessedUp.size(); i != e; ++i) {
  53. if (MessedUp[i] != ' ' || MessedUp[i - 1] != ' ')
  54. WithoutWhitespace.push_back(MessedUp[i]);
  55. }
  56. return WithoutWhitespace;
  57. }
  58. } // end namespace test
  59. } // end namespace format
  60. } // end namespace clang
  61. #endif