xref: /freebsd/contrib/llvm-project/clang/lib/Format/QualifierAlignmentFixer.h (revision 9f44a47fd07924afc035991af15d84e6585dea4f)
1 //===--- LeftRightQualifierAlignmentFixer.h ------------------------------*- C++
2 //-*-===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// This file declares LeftRightQualifierAlignmentFixer, a TokenAnalyzer that
12 /// enforces either east or west const depending on the style.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_CLANG_LIB_FORMAT_QUALIFIERALIGNMENTFIXER_H
17 #define LLVM_CLANG_LIB_FORMAT_QUALIFIERALIGNMENTFIXER_H
18 
19 #include "TokenAnalyzer.h"
20 
21 namespace clang {
22 namespace format {
23 
24 typedef std::function<std::pair<tooling::Replacements, unsigned>(
25     const Environment &)>
26     AnalyzerPass;
27 
28 class QualifierAlignmentFixer : public TokenAnalyzer {
29   // Left to Right ordering requires multiple passes
30   SmallVector<AnalyzerPass, 8> Passes;
31   StringRef &Code;
32   ArrayRef<tooling::Range> Ranges;
33   unsigned FirstStartColumn;
34   unsigned NextStartColumn;
35   unsigned LastStartColumn;
36   StringRef FileName;
37 
38 public:
39   QualifierAlignmentFixer(const Environment &Env, const FormatStyle &Style,
40                           StringRef &Code, ArrayRef<tooling::Range> Ranges,
41                           unsigned FirstStartColumn, unsigned NextStartColumn,
42                           unsigned LastStartColumn, StringRef FileName);
43 
44   std::pair<tooling::Replacements, unsigned>
45   analyze(TokenAnnotator &Annotator,
46           SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
47           FormatTokenLexer &Tokens) override;
48 
49   static void PrepareLeftRightOrdering(const std::vector<std::string> &Order,
50                                        std::vector<std::string> &LeftOrder,
51                                        std::vector<std::string> &RightOrder,
52                                        std::vector<tok::TokenKind> &Qualifiers);
53 };
54 
55 class LeftRightQualifierAlignmentFixer : public TokenAnalyzer {
56   std::string Qualifier;
57   bool RightAlign;
58   SmallVector<tok::TokenKind, 8> QualifierTokens;
59   std::vector<tok::TokenKind> ConfiguredQualifierTokens;
60 
61 public:
62   LeftRightQualifierAlignmentFixer(
63       const Environment &Env, const FormatStyle &Style,
64       const std::string &Qualifier,
65       const std::vector<tok::TokenKind> &ConfiguredQualifierTokens,
66       bool RightAlign);
67 
68   std::pair<tooling::Replacements, unsigned>
69   analyze(TokenAnnotator &Annotator,
70           SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
71           FormatTokenLexer &Tokens) override;
72 
73   static tok::TokenKind getTokenFromQualifier(const std::string &Qualifier);
74 
75   const FormatToken *analyzeRight(const SourceManager &SourceMgr,
76                                   const AdditionalKeywords &Keywords,
77                                   tooling::Replacements &Fixes,
78                                   const FormatToken *Tok,
79                                   const std::string &Qualifier,
80                                   tok::TokenKind QualifierType);
81 
82   const FormatToken *analyzeLeft(const SourceManager &SourceMgr,
83                                  const AdditionalKeywords &Keywords,
84                                  tooling::Replacements &Fixes,
85                                  const FormatToken *Tok,
86                                  const std::string &Qualifier,
87                                  tok::TokenKind QualifierType);
88 
89   // is the Token a simple or qualifier type
90   static bool isQualifierOrType(const FormatToken *Tok,
91                                 const std::vector<tok::TokenKind> &Qualifiers);
92 
93   // is the Token likely a Macro
94   static bool isPossibleMacro(const FormatToken *Tok);
95 };
96 
97 } // end namespace format
98 } // end namespace clang
99 
100 #endif
101