Cppcheck
standards.h
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2024 Cppcheck team.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 //---------------------------------------------------------------------------
20 #ifndef standardsH
21 #define standardsH
22 //---------------------------------------------------------------------------
23 
24 #include "utils.h"
25 
26 #include <string>
27 
28 /// @addtogroup Core
29 /// @{
30 
31 
32 /**
33  * @brief This is just a container for standards settings.
34  * This struct contains all possible standards that cppcheck recognize.
35  */
36 struct Standards {
37  enum Language { None, C, CPP };
38 
39  /** C code standard */
40  enum cstd_t { C89, C99, C11, C17, C23, CLatest = C23 } c = CLatest;
41 
42  /** C++ code standard */
44 
45  /** --std value given on command line */
46  std::string stdValue;
47 
48  bool setC(const std::string& str) {
49  stdValue = str;
50  if (str == "c89" || str == "C89") {
51  c = C89;
52  return true;
53  }
54  if (str == "c99" || str == "C99") {
55  c = C99;
56  return true;
57  }
58  if (str == "c11" || str == "C11") {
59  c = C11;
60  return true;
61  }
62  return false;
63  }
64  std::string getC() const {
65  switch (c) {
66  case C89:
67  return "c89";
68  case C99:
69  return "c99";
70  case C11:
71  return "c11";
72  case C17:
73  return "c17";
74  case C23:
75  return "c23";
76  }
77  return "";
78  }
79  static cstd_t getC(const std::string &std) {
80  if (std == "c89") {
81  return Standards::C89;
82  }
83  if (std == "c99") {
84  return Standards::C99;
85  }
86  if (std == "c11") {
87  return Standards::C11;
88  }
89  if (std == "c17") {
90  return Standards::C17;
91  }
92  if (std == "c23") {
93  return Standards::C23;
94  }
95  return Standards::CLatest;
96  }
97  bool setCPP(std::string str) {
98  stdValue = str;
99  strTolower(str);
100  cpp = getCPP(str);
101  return !stdValue.empty() && str == getCPP();
102  }
103  std::string getCPP() const {
104  return getCPP(cpp);
105  }
106  static std::string getCPP(cppstd_t std) {
107  switch (std) {
108  case CPP03:
109  return "c++03";
110  case CPP11:
111  return "c++11";
112  case CPP14:
113  return "c++14";
114  case CPP17:
115  return "c++17";
116  case CPP20:
117  return "c++20";
118  case CPP23:
119  return "c++23";
120  }
121  return "";
122  }
123  static cppstd_t getCPP(const std::string &std) {
124  if (std == "c++03") {
125  return Standards::CPP03;
126  }
127  if (std == "c++11") {
128  return Standards::CPP11;
129  }
130  if (std == "c++14") {
131  return Standards::CPP14;
132  }
133  if (std == "c++17") {
134  return Standards::CPP17;
135  }
136  if (std == "c++20") {
137  return Standards::CPP20;
138  }
139  if (std == "c++23") {
140  return Standards::CPP23;
141  }
142  return Standards::CPPLatest;
143  }
144 };
145 
146 /// @}
147 //---------------------------------------------------------------------------
148 #endif // standardsH
This is just a container for standards settings.
Definition: standards.h:36
static cppstd_t getCPP(const std::string &std)
Definition: standards.h:123
cstd_t
C code standard.
Definition: standards.h:40
bool setC(const std::string &str)
Definition: standards.h:48
std::string getC() const
Definition: standards.h:64
std::string stdValue
–std value given on command line
Definition: standards.h:46
enum Standards::cstd_t c
bool setCPP(std::string str)
Definition: standards.h:97
static cstd_t getC(const std::string &std)
Definition: standards.h:79
std::string getCPP() const
Definition: standards.h:103
static std::string getCPP(cppstd_t std)
Definition: standards.h:106
enum Standards::cppstd_t cpp
cppstd_t
C++ code standard.
Definition: standards.h:43
@ CPPLatest
Definition: standards.h:43
void strTolower(std::string &str)
Definition: utils.cpp:124