Skip to content

Commit 530aec1

Browse files
authored
Move C specific rules in src.c (#449)
Part of #447.
1 parent 63cf100 commit 530aec1

25 files changed

Lines changed: 56 additions & 38 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ foreach(_rule_dir IN LISTS rule_subdirs)
225225
get_filename_component(_rule_name ${_rule_dir} NAME)
226226
set(_out_dir ${RULES_IR_DIR}/${_rule_name})
227227
set(_cpp_out ${_out_dir}/ir_src.json)
228-
set(_cxxflags)
228+
set(_cxxflags --cxxflags=-isystem${_rule_dir})
229229
set(_cxxflags_file)
230230
if(EXISTS ${_rule_dir}/cxxflags)
231231
set(_cxxflags_file ${_rule_dir}/cxxflags)

‎cpp2rust/converter/printer.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ std::string ToString(clang::ASTContext &ctx, clang::QualType qual_type,
128128
clang::dyn_cast<clang::DecltypeType>(t.getTypePtr())) {
129129
t = decltype_type->getUnderlyingType();
130130
}
131+
if (const auto *typeof_type =
132+
clang::dyn_cast<clang::TypeOfExprType>(t.getTypePtr())) {
133+
t = typeof_type->getUnderlyingExpr()->getType();
134+
}
131135
if (const auto *typedef_type = t->getAs<clang::TypedefType>()) {
132136
if (t.getCanonicalType()->isBuiltinType()) {
133137
return typedef_type->getDecl()->getNameAsString();

‎cpp2rust/cpp_rule_preprocessor.cpp‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,14 @@ class Callback : public clang::ast_matchers::MatchFinder::MatchCallback {
117117
}
118118
return;
119119
}
120+
if (auto var = R.Nodes.getNodeAs<clang::VarDecl>("fvar")) {
121+
out_.try_emplace(
122+
var->getQualifiedNameAsString(),
123+
Printer::ToString(*R.Context,
124+
R.Nodes.getNodeAs<clang::FunctionDecl>("fn")));
125+
return;
126+
}
127+
120128
if (auto var = R.Nodes.getNodeAs<clang::TypedefNameDecl>("tvar")) {
121129
clang::QualType type = var->getUnderlyingType();
122130
if (auto *alias = llvm::dyn_cast<clang::TypeAliasDecl>(var)) {
@@ -999,6 +1007,13 @@ class ActionFactory : public clang::tooling::FrontendActionFactory {
9991007
.bind("tvar"),
10001008
&cb_);
10011009

1010+
finder_.addMatcher(
1011+
varDecl(matchesName("(^|::)f[0-9]+$"), isExpansionInMainFile(),
1012+
hasInitializer(ignoringImplicit(ignoringParenImpCasts(
1013+
declRefExpr(to(functionDecl().bind("fn")))))))
1014+
.bind("fvar"),
1015+
&cb_);
1016+
10021017
finder_.addMatcher(functionDecl(isDefinition(),
10031018
matchesName("(^|::)f[0-9]+$"),
10041019
isExpansionInMainFile())
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// Copyright (c) 2022-present INESC-ID.
22
// Distributed under the MIT license that can be found in the LICENSE file.
33

4-
#include "brotli/decode.h"
5-
#include "brotli/encode.h"
6-
7-
using t1 = BrotliDecoderResult;
8-
using t2 = BrotliEncoderMode;
9-
using t3 = BrotliDecoderStateStruct *;
10-
using t4 = const BrotliDecoderStateStruct *;
11-
using t5 = BrotliDecoderErrorCode;
4+
#include <brotli/decode.h>
5+
#include <brotli/encode.h>
6+
7+
typedef BrotliDecoderResult t1;
8+
typedef BrotliEncoderMode t2;
9+
typedef struct BrotliDecoderStateStruct *t3;
10+
typedef const struct BrotliDecoderStateStruct *t4;
11+
typedef BrotliDecoderErrorCode t5;
1212

1313
BrotliEncoderMode f1() { return BROTLI_MODE_FONT; }
1414

‎rules/cstddef/src.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
#include <stddef.h>
5+
#include <sys/types.h>
6+
7+
typedef size_t t2;
8+
typedef size_t *t3;
9+
typedef const size_t *t4;
10+
typedef __typeof__(sizeof(0)) t5;
11+
typedef ssize_t t6;
12+
typedef ssize_t *t7;
13+
typedef const ssize_t *t8;

‎rules/cstddef/src.cpp‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,9 @@
22
// Distributed under the MIT license that can be found in the LICENSE file.
33

44
#include <cstddef>
5-
#include <sys/types.h>
65

76
using t1 = std::byte;
87

9-
typedef size_t t2;
10-
typedef size_t *t3;
11-
typedef const size_t *t4;
12-
typedef decltype(sizeof(0)) t5;
13-
typedef ssize_t t6;
14-
typedef ssize_t *t7;
15-
typedef const ssize_t *t8;
16-
178
std::byte f1(const std::byte &a0, unsigned a1) { return operator<<(a0, a1); }
189

1910
std::byte f2(const std::byte &a0, unsigned a1) { return operator>>(a0, a1); }
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
#include <dirent.h>
55

6-
using t1 = DIR *;
7-
using t2 = struct dirent;
6+
typedef DIR *t1;
7+
typedef struct dirent t2;
88

99
DIR *f1(const char *name) { return opendir(name); }
1010

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
// Copyright (c) 2022-present INESC-ID.
22
// Distributed under the MIT license that can be found in the LICENSE file.
33

4+
#define _GNU_SOURCE
45
#include <fcntl.h>
56

6-
template <typename... Args>
7-
int f1(int a0, int a1, Args... args) {
8-
return fcntl(a0, a1, args...);
9-
}
7+
int (*f1)(int, int, ...) = fcntl;
108

11-
template <typename... Args>
12-
int f2(const char *a0, int a1, Args... args) {
13-
return open(a0, a1, args...);
14-
}
9+
int (*f2)(const char *, int, ...) = open;
1510

1611
int f3(void) { return O_CREAT; }
1712
int f4(void) { return O_TRUNC; }

0 commit comments

Comments
 (0)