Skip to content

Commit 3e2f72b

Browse files
committed
Address string related compiler compatibility issues.
This commit restructures the string, string.h and cstring headers to match the expected headers and namespaces of C++ standard declarations. This addresses compatibility issues with real compilers because our test cases expected to access C string functionality through the "#include <string>" header include, which is not the case in practice. This commit addresses compiler compatibility issues in the following rules: * A12-0-2 * A27-0-2 * M16-0-5 * M18-0-5 * DCL55-CPP * EXP62-CPP * OOP57-CPP * STR50-CPP
1 parent 84f9086 commit 3e2f72b

File tree

16 files changed

+295
-275
lines changed

16 files changed

+295
-275
lines changed

cpp/autosar/test/rules/A12-0-2/test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include <cstdlib>
2-
#include <string>
2+
#include <cstring>
33

44
class A {
55
public:

cpp/autosar/test/rules/M18-0-5/test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <string>
1+
#include <cstring>
22

33
void test_unbounded_str_funs() {
44
char str1[] = "Sample string";

cpp/cert/test/rules/EXP62-CPP/test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <string>
1+
#include <cstring>
22

33
struct S1 {
44
int i, j, k;

cpp/cert/test/rules/OOP57-CPP/test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <string>
1+
#include <cstring>
22

33
class trivial {};
44

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,29 @@
11
#ifndef _GHLIBCPP_CSTRING
22
#define _GHLIBCPP_CSTRING
3-
typedef unsigned long size_t;
3+
4+
#include <string.h>
5+
46
namespace std {
5-
void *memcpy(void *, const void *, size_t);
6-
size_t strlen(const char *);
7+
using ::memcmp;
8+
using ::memcpy;
9+
using ::memmove;
10+
using ::memset;
11+
using ::size_t;
12+
using ::strcat;
13+
using ::strchr;
14+
using ::strcmp;
15+
using ::strcoll;
16+
using ::strcpy;
17+
using ::strcspn;
18+
using ::strlen;
19+
using ::strncat;
20+
using ::strncmp;
21+
using ::strncpy;
22+
using ::strpbrk;
23+
using ::strrchr;
24+
using ::strspn;
25+
using ::strstr;
26+
using ::strtok;
27+
using ::strxfrm;
728
} // namespace std
829
#endif // _GHLIBCPP_CSTRING

cpp/common/test/includes/standard-library/istream.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#ifndef _GHLIBCPP_ISTREAM
22
#define _GHLIBCPP_ISTREAM
33

4-
#include "ios.h"
5-
#include "string.h"
4+
#include <ios>
5+
#include <string>
66

77
namespace std {
88
template <class charT, class traits>
Lines changed: 250 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,250 @@
1-
#include <string.h>
1+
#ifndef _GHLIBCPP_STRING
2+
#define _GHLIBCPP_STRING
3+
#include "initializer_list"
4+
#include "iosfwd.h"
5+
#include "iterator.h"
6+
#include "stddef.h"
7+
8+
namespace std {
9+
template <class charT> struct char_traits;
10+
11+
template <class T> class allocator {
12+
public:
13+
allocator() throw();
14+
typedef size_t size_type;
15+
};
16+
17+
template <class charT, class traits = char_traits<charT>,
18+
class Allocator = allocator<charT>>
19+
class basic_string {
20+
public:
21+
using value_type = charT;
22+
using reference = value_type &;
23+
using const_reference = const value_type &;
24+
typedef typename Allocator::size_type size_type;
25+
static const size_type npos = -1;
26+
27+
basic_string() : basic_string(Allocator()) {}
28+
explicit basic_string(const Allocator &a);
29+
basic_string(const basic_string &str);
30+
basic_string(basic_string &&str) noexcept;
31+
basic_string(const charT *s, size_type n, const Allocator &a = Allocator());
32+
basic_string(const charT *s, const Allocator &a = Allocator());
33+
basic_string(size_type n, charT c, const Allocator &a = Allocator());
34+
template <class InputIterator>
35+
basic_string(InputIterator begin, InputIterator end,
36+
const Allocator &a = Allocator());
37+
38+
~basic_string();
39+
basic_string &operator=(const basic_string &str);
40+
basic_string &operator=(basic_string &&str) noexcept;
41+
basic_string &operator=(const charT *s);
42+
basic_string &operator=(charT c);
43+
basic_string &operator=(initializer_list<charT>);
44+
45+
const charT *c_str() const;
46+
charT *data() noexcept;
47+
size_type size() const noexcept;
48+
size_type length() const noexcept;
49+
50+
typedef __iterator<charT> iterator;
51+
typedef __iterator<const charT> const_iterator;
52+
53+
iterator begin();
54+
iterator end();
55+
const_iterator begin() const;
56+
const_iterator end() const;
57+
const_iterator cbegin() const;
58+
const_iterator cend() const;
59+
60+
const charT &front() const;
61+
charT &front();
62+
const charT &back() const;
63+
charT &back();
64+
65+
const_reference operator[](size_type pos) const;
66+
reference operator[](size_type pos);
67+
const_reference at(size_type n) const;
68+
reference at(size_type n);
69+
basic_string &operator+=(const basic_string &str);
70+
basic_string &operator+=(const charT *s);
71+
basic_string &operator+=(charT c);
72+
basic_string &operator+=(initializer_list<charT>);
73+
basic_string &append(const basic_string &str);
74+
basic_string &append(const basic_string &str, size_type pos,
75+
size_type n = npos);
76+
basic_string &append(const charT *s, size_type n);
77+
basic_string &append(const charT *s);
78+
basic_string &append(size_type n, charT c);
79+
template <class InputIterator>
80+
basic_string &append(InputIterator first, InputIterator last);
81+
basic_string &append(initializer_list<charT>);
82+
void push_back(charT c);
83+
basic_string &assign(const basic_string &str);
84+
basic_string &assign(basic_string &&str) noexcept;
85+
basic_string &assign(const basic_string &str, size_type pos,
86+
size_type n = npos);
87+
basic_string &assign(const charT *s, size_type n);
88+
basic_string &assign(const charT *s);
89+
basic_string &assign(size_type n, charT c);
90+
template <class InputIterator>
91+
basic_string &assign(InputIterator first, InputIterator last);
92+
basic_string &assign(initializer_list<charT>);
93+
basic_string &insert(size_type pos1, const basic_string &str);
94+
basic_string &insert(size_type pos1, const basic_string &str, size_type pos2,
95+
size_type n = npos);
96+
basic_string &insert(size_type pos, const charT *s, size_type n);
97+
basic_string &insert(size_type pos, const charT *s);
98+
basic_string &insert(size_type pos, size_type n, charT c);
99+
iterator insert(const_iterator p, charT c);
100+
iterator insert(const_iterator p, size_type n, charT c);
101+
template <class InputIterator>
102+
iterator insert(const_iterator p, InputIterator first, InputIterator last);
103+
iterator insert(const_iterator p, initializer_list<charT>);
104+
basic_string &erase(size_type pos = 0, size_type n = npos);
105+
iterator erase(const_iterator p);
106+
iterator erase(const_iterator first, const_iterator last);
107+
basic_string &replace(size_type pos1, size_type n1, const basic_string &str);
108+
basic_string &replace(size_type pos1, size_type n1, const basic_string &str,
109+
size_type pos2, size_type n2 = npos);
110+
basic_string &replace(size_type pos, size_type n1, const charT *s,
111+
size_type n2);
112+
basic_string &replace(size_type pos, size_type n1, const charT *s);
113+
basic_string &replace(size_type pos, size_type n1, size_type n2, charT c);
114+
basic_string &replace(const_iterator i1, const_iterator i2,
115+
const basic_string &str);
116+
basic_string &replace(const_iterator i1, const_iterator i2, const charT *s,
117+
size_type n);
118+
basic_string &replace(const_iterator i1, const_iterator i2, const charT *s);
119+
basic_string &replace(const_iterator i1, const_iterator i2, size_type n,
120+
charT c);
121+
template <class InputIterator>
122+
basic_string &replace(const_iterator i1, const_iterator i2, InputIterator j1,
123+
InputIterator j2);
124+
basic_string &replace(const_iterator, const_iterator,
125+
initializer_list<charT>);
126+
127+
size_type copy(charT *s, size_type n, size_type pos = 0) const;
128+
void clear() noexcept;
129+
void swap(basic_string &s) noexcept;
130+
131+
size_type find(const basic_string &str, size_type pos = 0) const noexcept;
132+
size_type find(const charT *s, size_type pos, size_type n) const;
133+
size_type find(const charT *s, size_type pos = 0) const;
134+
size_type find(charT c, size_type pos = 0) const;
135+
size_type rfind(const basic_string &str, size_type pos = npos) const noexcept;
136+
size_type rfind(const charT *s, size_type pos, size_type n) const;
137+
size_type rfind(const charT *s, size_type pos = npos) const;
138+
size_type rfind(charT c, size_type pos = npos) const;
139+
size_type find_first_of(const basic_string &str,
140+
size_type pos = 0) const noexcept;
141+
size_type find_first_of(const charT *s, size_type pos, size_type n) const;
142+
size_type find_first_of(const charT *s, size_type pos = 0) const;
143+
size_type find_first_of(charT c, size_type pos = 0) const;
144+
size_type find_last_of(const basic_string &str,
145+
size_type pos = npos) const noexcept;
146+
size_type find_last_of(const charT *s, size_type pos, size_type n) const;
147+
size_type find_last_of(const charT *s, size_type pos = npos) const;
148+
size_type find_last_of(charT c, size_type pos = npos) const;
149+
size_type find_first_not_of(const basic_string &str,
150+
size_type pos = 0) const noexcept;
151+
size_type find_first_not_of(const charT *s, size_type pos, size_type n) const;
152+
size_type find_first_not_of(const charT *s, size_type pos = 0) const;
153+
size_type find_first_not_of(charT c, size_type pos = 0) const;
154+
size_type find_last_not_of(const basic_string &str,
155+
156+
size_type pos = npos) const noexcept;
157+
size_type find_last_not_of(const charT *s, size_type pos, size_type n) const;
158+
size_type find_last_not_of(const charT *s, size_type pos = npos) const;
159+
size_type find_last_not_of(charT c, size_type pos = npos) const;
160+
basic_string substr(size_type pos = 0, size_type n = npos) const;
161+
int compare(const basic_string &str) const noexcept;
162+
int compare(size_type pos1, size_type n1, const basic_string &str) const;
163+
int compare(size_type pos1, size_type n1, const basic_string &str,
164+
size_type pos2, size_type n2 = npos) const;
165+
int compare(const charT *s) const;
166+
int compare(size_type pos1, size_type n1, const charT *s) const;
167+
int compare(size_type pos1, size_type n1, const charT *s, size_type n2) const;
168+
};
169+
170+
template <class charT, class traits, class Allocator>
171+
basic_string<charT, traits, Allocator>
172+
operator+(const basic_string<charT, traits, Allocator> &lhs,
173+
const basic_string<charT, traits, Allocator> &rhs);
174+
template <class charT, class traits, class Allocator>
175+
basic_string<charT, traits, Allocator>
176+
operator+(const basic_string<charT, traits, Allocator> &lhs, const charT *rhs);
177+
template <class charT, class traits, class Allocator>
178+
basic_string<charT, traits, Allocator>
179+
operator+(const charT *lhs, const basic_string<charT, traits, Allocator> &rhs);
180+
181+
template <class charT, class traits, class Allocator>
182+
bool operator==(const basic_string<charT, traits, Allocator> &lhs,
183+
const basic_string<charT, traits, Allocator> &rhs) noexcept;
184+
template <class charT, class traits, class Allocator>
185+
bool operator==(const charT *lhs,
186+
const basic_string<charT, traits, Allocator> &rhs);
187+
template <class charT, class traits, class Allocator>
188+
bool operator==(const basic_string<charT, traits, Allocator> &lhs,
189+
const charT *rhs);
190+
template <class charT, class traits, class Allocator>
191+
bool operator!=(const basic_string<charT, traits, Allocator> &lhs,
192+
const basic_string<charT, traits, Allocator> &rhs) noexcept;
193+
template <class charT, class traits, class Allocator>
194+
bool operator!=(const charT *lhs,
195+
const basic_string<charT, traits, Allocator> &rhs);
196+
template <class charT, class traits, class Allocator>
197+
bool operator!=(const basic_string<charT, traits, Allocator> &lhs,
198+
const charT *rhs);
199+
template <class charT, class traits, class Allocator>
200+
bool operator<(const basic_string<charT, traits, Allocator> &lhs,
201+
const basic_string<charT, traits, Allocator> &rhs) noexcept;
202+
template <class charT, class traits, class Allocator>
203+
bool operator<(const basic_string<charT, traits, Allocator> &lhs,
204+
const charT *rhs);
205+
template <class charT, class traits, class Allocator>
206+
bool operator<(const charT *lhs,
207+
const basic_string<charT, traits, Allocator> &rhs);
208+
template <class charT, class traits, class Allocator>
209+
bool operator>(const basic_string<charT, traits, Allocator> &lhs,
210+
const basic_string<charT, traits, Allocator> &rhs) noexcept;
211+
template <class charT, class traits, class Allocator>
212+
bool operator>(const basic_string<charT, traits, Allocator> &lhs,
213+
const charT *rhs);
214+
template <class charT, class traits, class Allocator>
215+
bool operator>(const charT *lhs,
216+
const basic_string<charT, traits, Allocator> &rhs);
217+
template <class charT, class traits, class Allocator>
218+
bool operator<=(const basic_string<charT, traits, Allocator> &lhs,
219+
const basic_string<charT, traits, Allocator> &rhs) noexcept;
220+
template <class charT, class traits, class Allocator>
221+
bool operator<=(const basic_string<charT, traits, Allocator> &lhs,
222+
const charT *rhs);
223+
template <class charT, class traits, class Allocator>
224+
bool operator<=(const charT *lhs,
225+
const basic_string<charT, traits, Allocator> &rhs);
226+
template <class charT, class traits, class Allocator>
227+
bool operator>=(const basic_string<charT, traits, Allocator> &lhs,
228+
const basic_string<charT, traits, Allocator> &rhs) noexcept;
229+
template <class charT, class traits, class Allocator>
230+
bool operator>=(const basic_string<charT, traits, Allocator> &lhs,
231+
const charT *rhs);
232+
template <class charT, class traits, class Allocator>
233+
bool operator>=(const charT *lhs,
234+
const basic_string<charT, traits, Allocator> &rhs);
235+
236+
typedef basic_string<char> string;
237+
238+
int stoi(const string &str, size_t *idx = 0, int base = 10);
239+
long stol(const string &str, size_t *idx = 0, int base = 10);
240+
unsigned long stoul(const string &str, size_t *idx = 0, int base = 10);
241+
long long stoll(const string &str, size_t *idx = 0, int base = 10);
242+
unsigned long long stoull(const string &str, size_t *idx = 0, int base = 10);
243+
float stof(const string &str, size_t *idx = 0);
244+
double stod(const string &str, size_t *idx = 0);
245+
long double stold(const string &str, size_t *idx = 0);
246+
247+
std::string to_string(int value);
248+
} // namespace std
249+
250+
#endif // _GHLIBCPP_STRING

0 commit comments

Comments
 (0)