Macros.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //===--- MacroExpander.h - Format C++ code ----------------------*- C++ -*-===//
  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. /// \file
  10. /// This file contains the main building blocks of macro support in
  11. /// clang-format.
  12. ///
  13. /// In order to not violate the requirement that clang-format can format files
  14. /// in isolation, clang-format's macro support uses expansions users provide
  15. /// as part of clang-format's style configuration.
  16. ///
  17. /// Macro definitions are of the form "MACRO(p1, p2)=p1 + p2", but only support
  18. /// one level of expansion (\see MacroExpander for a full description of what
  19. /// is supported).
  20. ///
  21. /// As part of parsing, clang-format uses the MacroExpander to expand the
  22. /// spelled token streams into expanded token streams when it encounters a
  23. /// macro call. The UnwrappedLineParser continues to parse UnwrappedLines
  24. /// from the expanded token stream.
  25. /// After the expanded unwrapped lines are parsed, the MacroUnexpander matches
  26. /// the spelled token stream into unwrapped lines that best resemble the
  27. /// structure of the expanded unwrapped lines.
  28. ///
  29. /// When formatting, clang-format formats the expanded unwrapped lines first,
  30. /// determining the token types. Next, it formats the spelled unwrapped lines,
  31. /// keeping the token types fixed, while allowing other formatting decisions
  32. /// to change.
  33. ///
  34. //===----------------------------------------------------------------------===//
  35. #ifndef CLANG_LIB_FORMAT_MACROS_H
  36. #define CLANG_LIB_FORMAT_MACROS_H
  37. #include <string>
  38. #include <unordered_map>
  39. #include <vector>
  40. #include "Encoding.h"
  41. #include "FormatToken.h"
  42. #include "llvm/ADT/ArrayRef.h"
  43. #include "llvm/ADT/SmallVector.h"
  44. #include "llvm/ADT/StringRef.h"
  45. namespace llvm {
  46. class MemoryBuffer;
  47. } // namespace llvm
  48. namespace clang {
  49. class IdentifierTable;
  50. class SourceManager;
  51. namespace format {
  52. struct FormatStyle;
  53. /// Takes a set of macro definitions as strings and allows expanding calls to
  54. /// those macros.
  55. ///
  56. /// For example:
  57. /// Definition: A(x, y)=x + y
  58. /// Call : A(int a = 1, 2)
  59. /// Expansion : int a = 1 + 2
  60. ///
  61. /// Expansion does not check arity of the definition.
  62. /// If fewer arguments than expected are provided, the remaining parameters
  63. /// are considered empty:
  64. /// Call : A(a)
  65. /// Expansion: a +
  66. /// If more arguments than expected are provided, they will be discarded.
  67. ///
  68. /// The expander does not support:
  69. /// - recursive expansion
  70. /// - stringification
  71. /// - concatenation
  72. /// - variadic macros
  73. ///
  74. /// Furthermore, only a single expansion of each macro argument is supported,
  75. /// so that we cannot get conflicting formatting decisions from different
  76. /// expansions.
  77. /// Definition: A(x)=x+x
  78. /// Call : A(id)
  79. /// Expansion : id+x
  80. ///
  81. class MacroExpander {
  82. public:
  83. using ArgsList = llvm::ArrayRef<llvm::SmallVector<FormatToken *, 8>>;
  84. /// Construct a macro expander from a set of macro definitions.
  85. /// Macro definitions must be encoded as UTF-8.
  86. ///
  87. /// Each entry in \p Macros must conform to the following simple
  88. /// macro-definition language:
  89. /// <definition> ::= <id> <expansion> | <id> "(" <params> ")" <expansion>
  90. /// <params> ::= <id-list> | ""
  91. /// <id-list> ::= <id> | <id> "," <params>
  92. /// <expansion> ::= "=" <tail> | <eof>
  93. /// <tail> ::= <tok> <tail> | <eof>
  94. ///
  95. /// Macros that cannot be parsed will be silently discarded.
  96. ///
  97. MacroExpander(const std::vector<std::string> &Macros,
  98. clang::SourceManager &SourceMgr, const FormatStyle &Style,
  99. llvm::SpecificBumpPtrAllocator<FormatToken> &Allocator,
  100. IdentifierTable &IdentTable);
  101. ~MacroExpander();
  102. /// Returns whether a macro \p Name is defined.
  103. bool defined(llvm::StringRef Name) const;
  104. /// Returns whether the macro has no arguments and should not consume
  105. /// subsequent parentheses.
  106. bool objectLike(llvm::StringRef Name) const;
  107. /// Returns the expanded stream of format tokens for \p ID, where
  108. /// each element in \p Args is a positional argument to the macro call.
  109. llvm::SmallVector<FormatToken *, 8> expand(FormatToken *ID,
  110. ArgsList Args) const;
  111. private:
  112. struct Definition;
  113. class DefinitionParser;
  114. void parseDefinition(const std::string &Macro);
  115. clang::SourceManager &SourceMgr;
  116. const FormatStyle &Style;
  117. llvm::SpecificBumpPtrAllocator<FormatToken> &Allocator;
  118. IdentifierTable &IdentTable;
  119. std::vector<std::unique_ptr<llvm::MemoryBuffer>> Buffers;
  120. llvm::StringMap<Definition> Definitions;
  121. };
  122. } // namespace format
  123. } // namespace clang
  124. #endif