summaryrefslogtreecommitdiffstats
path: root/debian/uncrustify-trinity/uncrustify-trinity-0.74.0/src/option.h
blob: 4ab356b57771fe75dd21e6084cbc767abea13d65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/**
 * @file option.h
 * Enumerations and data types for options.
 *
 * @author  Ben Gardner
 * @author  Guy Maurel since version 0.62 for uncrustify4Qt
 *          October 2015, 2016
 * @author  Matthew Woehlke since version 0.67
 * @license GPL v2+
 */
#ifndef OPTION_H_INCLUDED
#define OPTION_H_INCLUDED

/* NOTE:
 * This file is processed by make_option_enum.py, which parses any 'enum class'
 * it finds, as well as the special macros UNC_OPTVAL_ALIAS and UNC_OPTVALS.
 *
 * The '// <PREFIX>' comment after an 'enum class' tells the script to generate
 * aliases for the enum values using the prefix that is given in the '<>'s.
 * Don't remove or alter these.
 */

#include "enum_flags.h"

#include <string>
#include <vector>

#include <cassert>

#ifdef IGNORE // WinBase.h
#undef IGNORE
#endif

namespace uncrustify
{

template<typename T> class Option;

//-----------------------------------------------------------------------------
// Option types
enum class option_type_e // <OT>
{
   // UNC_CONVERT_INTERNAL
   BOOL,
   IARF,
   LINEEND,
   TOKENPOS,
   NUM,
   UNUM,
   STRING,
};

#if 0 // Fake enumeration for make_option_enum.py
enum class bool
{
   true,
   false,
};
#endif

//-----------------------------------------------------------------------------
/// I/A/R/F values - these are bit fields
enum class iarf_e // <IARF>
{
   IGNORE      = 0,              //! option ignores a given feature
   ADD         = (1u << 0),      //! option adds a given feature
   REMOVE      = (1u << 1),      //! option removes a given feature
   FORCE       = (ADD | REMOVE), //! option forces the usage of a given feature
   NOT_DEFINED = (1u << 2)       //! for debugging
};

UNC_DECLARE_FLAGS(iarf_flags_t, iarf_e);
UNC_DECLARE_OPERATORS_FOR_FLAGS(iarf_flags_t);

//-----------------------------------------------------------------------------
/// Line endings
enum class line_end_e // <LE>
{
   LF,   //! "\n"   typically used on Unix/Linux system
   CRLF, //! "\r\n" typically used on Windows systems
   CR,   //! "\r"   carriage return without newline
   AUTO, //! keep last
};
constexpr auto line_end_styles = static_cast<size_t>(line_end_e::AUTO);

//-----------------------------------------------------------------------------
/// Token position - these are bit fields
enum class token_pos_e // <TP>
{
   IGNORE      = 0,               //! don't change it
   BREAK       = 1,               //! add a newline before or after the if not present
   FORCE       = 2,               //! force a newline on one side and not the other
   LEAD        = 4,               //! at the start of a line or leading if wrapped line
   TRAIL       = 8,               //! at the end of a line or trailing if wrapped line
   JOIN        = 16,              //! remove newlines on both sides
   LEAD_BREAK  = (LEAD | BREAK),  //  5
   LEAD_FORCE  = (LEAD | FORCE),  //  6
   TRAIL_BREAK = (TRAIL | BREAK), //  9
   TRAIL_FORCE = (TRAIL | FORCE), // 10
};

UNC_DECLARE_FLAGS(token_pos_flags_t, token_pos_e);
UNC_DECLARE_OPERATORS_FOR_FLAGS(token_pos_flags_t);

//-----------------------------------------------------------------------------
/// Abstract (untyped) interface for options
class GenericOption
{
public:
   GenericOption(const char *opt_name, const char *opt_desc)
      : m_name{opt_name}
      , m_desc{opt_desc}
   {}

   virtual ~GenericOption() = default;

   virtual option_type_e type() const = 0;
   const char *name() const { return(m_name); }
   const char *description() const { return(m_desc); }
   virtual const char *const *possibleValues() const = 0;

   virtual std::string defaultStr() const = 0;
   virtual std::string minStr() const { return(std::string{}); }
   virtual std::string maxStr() const { return(std::string{}); }

   virtual bool isDefault() const = 0;

   virtual void reset() = 0;
   virtual bool read(const char *s) = 0;
   virtual std::string str() const = 0;

protected:
   template<typename V> friend bool read_enum(const char *s, Option<V> &o);
   template<typename V> friend bool read_number(const char *s, Option<V> &o);

   void warnUnexpectedValue(const char *actual) const;
   void warnIncompatibleReference(const GenericOption *ref) const;

   const char *const m_name;
   const char *const m_desc;
};

//-----------------------------------------------------------------------------
// Helper class for reporting problems with options
class OptionWarning
{
public:
   enum class /* UNC_NO_META */ Severity
   {
      OS_CRITICAL,
      OS_MINOR,
   };

   constexpr static auto CRITICAL = Severity::OS_CRITICAL;
   constexpr static auto MINOR    = Severity::OS_MINOR;

   OptionWarning(const char *filename, Severity = CRITICAL);
   OptionWarning(const GenericOption *, Severity = CRITICAL);
   OptionWarning(const OptionWarning &) = delete;
   ~OptionWarning();

#ifdef __GNUC__
   [[gnu::format(printf, 2, 3)]]
#endif
   void operator()(const char *fmt, ...);
};

//-----------------------------------------------------------------------------
// Concrete (strongly typed) interface for options
template<typename T>
class Option : public GenericOption
{
public:
   Option(const char *opt_name, const char *opt_desc, T opt_val = T{})
      : GenericOption{opt_name, opt_desc}
      , m_val{opt_val}
      , m_default{opt_val}
   {}

   option_type_e type() const override;
   const char *const *possibleValues() const override;

   std::string defaultStr() const override;

   bool isDefault() const override { return(m_val == m_default); }

   //! resets option to its default value
   //- currently only used by the emscripten interface
   virtual void reset() override;

   bool read(const char *s) override;
   std::string str() const override;

   T operator()() const { return(m_val); }
   Option &operator=(T val) { m_val = val; return(*this); }

protected:
   template<typename V> friend bool read_enum(const char *s, Option<V> &o);
   template<typename V> friend bool read_number(const char *s, Option<V> &o);

   virtual bool validate(long) { return(true); }

   T m_val     = T{};
   T m_default = T{};
};

//-----------------------------------------------------------------------------
// Concrete (strongly typed) interface for bounded numeric options
template<typename T, T min, T max>
class BoundedOption : public Option<T>
{
public:
   BoundedOption(const char *opt_name, const char *opt_desc, T opt_val = T{})
      : Option<T>{opt_name, opt_desc, opt_val}
   {
      assert(  opt_val >= min
            && opt_val <= max);
   }

   std::string minStr() const override { return(std::to_string(min)); }
   std::string maxStr() const override { return(std::to_string(max)); }

protected:
   bool validate(long val) override
   {
      if (val < static_cast<long>(min))
      {
         OptionWarning w{ this };
         w("requested value %ld for option '%s' "
           "is less than the minimum value %ld",
           val, this->name(), static_cast<long>(min));
         return(false);
      }

      if (val > static_cast<long>(max))
      {
         OptionWarning w{ this };
         w("requested value %ld for option '%s' "
           "is greater than the maximum value %ld",
           val, this->name(), static_cast<long>(max));
         return(false);
      }
      return(true);
   }
};

///////////////////////////////////////////////////////////////////////////////

// Declaration of option types; implementations are in option.cpp
#define UNC_IMPLEMENT_OPTION(T)                                     \
   template<> option_type_e Option<T>::type() const;                \
   template<> const char *const *Option<T>::possibleValues() const; \
   template<> bool Option<T>::read(const char *s);                  \
   extern template class Option<T>

UNC_IMPLEMENT_OPTION(bool);
UNC_IMPLEMENT_OPTION(iarf_e);
UNC_IMPLEMENT_OPTION(line_end_e);
UNC_IMPLEMENT_OPTION(token_pos_e);
UNC_IMPLEMENT_OPTION(signed);
UNC_IMPLEMENT_OPTION(unsigned);
UNC_IMPLEMENT_OPTION(std::string);

// Additional mappings for option values
#define UNC_OPTVAL_ALIAS(...) \
   static_assert(true, "This is just a tag for make_option_enum.py")

UNC_OPTVAL_ALIAS(bool, false, "0", "f", "n", "no");
UNC_OPTVAL_ALIAS(bool, true, "1", "t", "y", "yes");
UNC_OPTVAL_ALIAS(iarf_e, IGNORE, "i");
UNC_OPTVAL_ALIAS(iarf_e, ADD, "a", "2", "t", "true", "y", "yes");
UNC_OPTVAL_ALIAS(iarf_e, REMOVE, "r", "0", "f", "false", "n", "no");
UNC_OPTVAL_ALIAS(iarf_e, FORCE, "f", "1");

// Possible values for options, by type
#define UNC_OPTVALS(e)    extern const char *const e ## _values[]
UNC_OPTVALS(iarf);
UNC_OPTVALS(line_end);
UNC_OPTVALS(token_pos);

extern bool convert_string(const char *, bool &);
extern bool convert_string(const char *, iarf_e &);
extern bool convert_string(const char *, line_end_e &);
extern bool convert_string(const char *, token_pos_e &);

extern const char *to_string(bool);
extern const char *to_string(iarf_e);
extern const char *to_string(line_end_e);
extern const char *to_string(token_pos_e);
extern const char *to_string(option_type_e);

struct OptionGroup
{
   const char                   *description;
   std::vector<GenericOption *> options;
};


/**
 * @brief Defines a new group of uncrustify options.
 *
 * New options are always added to the most recently defined group.
 */
void begin_option_group(const char *description);


/**
 * @brief Adds an uncrustify option to the global option registry.
 *
 * The option is added to the most recently defined option group.
 */
void register_option(GenericOption *);


GenericOption *find_option(const char *name);


//! Add all uncrustify options to the global option registry
void register_options(void);


OptionGroup *get_option_group(size_t);


size_t get_option_count();


/**
 * processes a single line string to extract configuration settings
 * increments cpd.line_number and cpd.error_count
 *
 * @param config_line  single line string that will be processed
 * @param filename     for log messages, file from which the \p config_line
 *                     param was extracted
 * @param compat_level version of Uncrustify with which to be compatible
 */
void process_option_line(const std::string &config_line, const char *filename, int &compat_level);


bool load_option_file(const char *filename, int compat_level = 0);


/**
 * save the used options into a text file
 *
 * @param pfile     file to print into
 * @param with_doc  also print description
 * @param minimal   print only options with non default value
 */
void save_option_file(FILE *pfile, bool with_doc = false, bool minimal = false);


/**
 * get the marker that was selected for the end of line via the config file
 *
 * @return "\n"     if newlines was set to LE_LF in the config file
 * @return "\r\n"   if newlines was set to LE_CRLF in the config file
 * @return "\r"     if newlines was set to LE_CR in the config file
 * @return "\n"     if newlines was set to LE_AUTO in the config file
 */
const char *get_eol_marker();

} // namespace uncrustify

#endif /* OPTION_H_INCLUDED */