Skip to content

Commit

Permalink
Script fixing up headers and the changed files due to this fix
Browse files Browse the repository at this point in the history
  • Loading branch information
yangchi committed Sep 6, 2024
1 parent 0083614 commit 85a8095
Show file tree
Hide file tree
Showing 40 changed files with 2,143 additions and 301 deletions.
2 changes: 1 addition & 1 deletion array.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
extern "C" {
#endif

#include "./alloc.h"
#include "alloc.h" // modified by Poolside fork

#include <assert.h>
#include <stdbool.h>
Expand Down
55 changes: 55 additions & 0 deletions cpp/alloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@


////////////////////////////////////////////////////////////////////////////////
// Poolside modification in the fork, generated at 2024-09-06 11:07:56.198057 //
////////////////////////////////////////////////////////////////////////////////


#ifndef TREE_SITTER_ALLOC_H_
#define TREE_SITTER_ALLOC_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#if defined(TREE_SITTER_HIDDEN_SYMBOLS) || defined(_WIN32)
#define TS_PUBLIC
#else
#define TS_PUBLIC __attribute__((visibility("default")))
#endif

TS_PUBLIC extern void *(*ts_current_malloc)(size_t);
TS_PUBLIC extern void *(*ts_current_calloc)(size_t, size_t);
TS_PUBLIC extern void *(*ts_current_realloc)(void *, size_t);
TS_PUBLIC extern void (*ts_current_free)(void *);

// Allow clients to override allocation functions
#ifndef ts_malloc
#define ts_malloc ts_current_malloc
#endif
#ifndef ts_calloc
#define ts_calloc ts_current_calloc
#endif
#ifndef ts_realloc
#define ts_realloc ts_current_realloc
#endif
#ifndef ts_free
#define ts_free ts_current_free
#endif

#ifdef __cplusplus
}
#endif

#endif // TREE_SITTER_ALLOC_H_


////////////////////////////////////////////////////////////////////////////////
// Poolside modification in the fork, generated at 2024-09-06 11:07:56.198057 //
////////////////////////////////////////////////////////////////////////////////


2 changes: 1 addition & 1 deletion cpp/scanner.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../alloc.h"
#include "alloc.h" // modified by Poolside fork
#include "parser.h"

#include <assert.h>
Expand Down
55 changes: 55 additions & 0 deletions csharp/alloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@


////////////////////////////////////////////////////////////////////////////////
// Poolside modification in the fork, generated at 2024-09-06 11:07:56.082258 //
////////////////////////////////////////////////////////////////////////////////


#ifndef TREE_SITTER_ALLOC_H_
#define TREE_SITTER_ALLOC_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#if defined(TREE_SITTER_HIDDEN_SYMBOLS) || defined(_WIN32)
#define TS_PUBLIC
#else
#define TS_PUBLIC __attribute__((visibility("default")))
#endif

TS_PUBLIC extern void *(*ts_current_malloc)(size_t);
TS_PUBLIC extern void *(*ts_current_calloc)(size_t, size_t);
TS_PUBLIC extern void *(*ts_current_realloc)(void *, size_t);
TS_PUBLIC extern void (*ts_current_free)(void *);

// Allow clients to override allocation functions
#ifndef ts_malloc
#define ts_malloc ts_current_malloc
#endif
#ifndef ts_calloc
#define ts_calloc ts_current_calloc
#endif
#ifndef ts_realloc
#define ts_realloc ts_current_realloc
#endif
#ifndef ts_free
#define ts_free ts_current_free
#endif

#ifdef __cplusplus
}
#endif

#endif // TREE_SITTER_ALLOC_H_


////////////////////////////////////////////////////////////////////////////////
// Poolside modification in the fork, generated at 2024-09-06 11:07:56.082258 //
////////////////////////////////////////////////////////////////////////////////


16 changes: 15 additions & 1 deletion php/tree_sitter/array.h → csharp/array.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@


////////////////////////////////////////////////////////////////////////////////
// Poolside modification in the fork, generated at 2024-09-06 11:07:56.082481 //
////////////////////////////////////////////////////////////////////////////////


#ifndef TREE_SITTER_ARRAY_H_
#define TREE_SITTER_ARRAY_H_

#ifdef __cplusplus
extern "C" {
#endif

#include "./alloc.h"
#include "alloc.h" // modified by Poolside fork

#include <assert.h>
#include <stdbool.h>
Expand Down Expand Up @@ -288,3 +295,10 @@ static inline void _array__splice(Array *self, size_t element_size,
#endif

#endif // TREE_SITTER_ARRAY_H_


////////////////////////////////////////////////////////////////////////////////
// Poolside modification in the fork, generated at 2024-09-06 11:07:56.082481 //
////////////////////////////////////////////////////////////////////////////////


4 changes: 2 additions & 2 deletions csharp/scanner.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "../alloc.h"
#include "../array.h"
#include "alloc.h" // modified by Poolside fork
#include "array.h" // modified by Poolside fork
#include "parser.h"

#include <wctype.h>
Expand Down
4 changes: 2 additions & 2 deletions get_changed_ranges.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
extern "C" {
#endif

#include "./tree_cursor.h"
#include "./subtree.h"
#include "tree_cursor.h" // modified by Poolside fork
#include "subtree.h" // modified by Poolside fork

typedef Array(TSRange) TSRangeArray;

Expand Down
146 changes: 146 additions & 0 deletions header_fix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#!/usr/bin/env python3

import datetime
import os
import shutil

lang_dirs = [
'ada',
'c',
'lua',
'csharp',
'cpp',
'elixir',
'elm',
'golang',
'java',
'javascript',
'ocaml',
'objc',
'php',
'python',
'ruby',
'rust',
#'typescript', Oh you are so special. You make me feel dumb
'kotlin',
'scala',
'zig'
]

# flatten a directory
def flatten_dir(root):
print(f'Flattening {root}')
for dir, _, filenames in os.walk(root):
if dir == root:
continue
for filename in filenames:
src = os.path.join(dir, filename)
dst = os.path.join(root, filename)
force_move_file(src, dst)

# list files in directory (not including its subdirs) that ends with any of the given extensions
def ls_exts(directory, extensions):
files = os.listdir(directory)
return [os.path.join(directory, file) for file in files
if os.path.isfile(os.path.join(directory, file)) and endswith_any(file, extensions)]


# fix_headers assumes some pre-processing is already done. Then it changes:
# #include "../foo/bar.h"
# #include "./bar.h"
# #include "foo/bar.h"
# all into #include "bar.h"
def fix_headers(file_path):
print(f'Fixing headers in {os.path.abspath(file_path)}')
file_dir = os.path.dirname(os.path.abspath(file_path))
with open(file_path, 'r') as file:
content = file.read()
new_content = []
needs_write = False
for line in content.split('\n'):
if not line.startswith('#include') or line.count('"') == 0:
new_content.append(line)
continue
header = line.split('"')[1]
idx = header.find('/')
if idx == -1:
new_content.append(line)
continue
needs_write = True
if header.startswith('../'):
copy_recur(os.path.join(file_dir, header), file_dir)
line = line.replace('../', '')
elif header.startswith('./'):
line = line.replace('./', '')
else:
# this is a big assumption: That is the header it tries to include here is in a subdir we already flattened
base = os.path.basename(header)
line = f'#include "{base}"'
line += ' // modified by Poolside fork'
new_content.append(line)
if needs_write:
with open(file_path, 'w') as file:
file.write('\n'.join(new_content))

def add_extra_line(file_path, extra_line):
with open(file_path, 'r') as file:
content = file.read()
with open(file_path, 'w') as file:
file.write(extra_line + content + extra_line)

# generate a comment header/footer to make it clear a file is modified by us, not directly from upstream
def make_comments():
comment_line = '////////////////////////////////////////////////////////////////////////////////\n'
return f'\n\n{comment_line}// Poolside modification in the fork, generated at {datetime.datetime.now()} //\n{comment_line}\n\n'


# move file from src to dst if src exists, overwrite dst if it exists.
def force_move_file(src, dst):
# if src no longer exists, we proabably done moving it already. Abort:
if not os.path.exists(src):
print(f'File {src} no longer exists, not going to move anything.')
return
# if dst exists, we want to overwrite it
if os.path.exists(dst):
print(f'Warning: removing existing file {dst}')
os.remove(dst)
os.rename(src, dst)
add_extra_line(dst, make_comments())
print(f'File {src} has been moved to {dst}.')


def endswith_any(filename, extenstions):
return any(filename.endswith(extension) for extension in extenstions)


def op_recur(src, dst_dir, operation):
headers = []
with open(src, 'r') as f:
for lines in f:
if lines.startswith('#include') and lines.count('"') > 0:
headers.append(lines.split('"')[1])
for h in headers:
op_recur(h, dst_dir, operation)
operation(src, os.path.join(dst_dir, os.path.basename(src)))


def copy_with_extra_commends(src, dst):
shutil.copy2(src, dst)
add_extra_line(dst, make_comments())

def copy_recur(src, dst_dir):
op_recur(src, dst_dir, copy_with_extra_commends)
print(f'File {src} has been copied to {dst_dir}.')


# (1) Fix up go-tree-sitter root level headers so they do not have #include "./foo.h"
# (2) Flatten a list of language subdirs that we care about. No more go-tree-sitter/{lang}/foo/bar.h
# (3) Then in these flatten subdirs, fix up any header inclusion that isn't directly a sibling file.
if __name__ == '__main__':
root_dir_headers = ls_exts(os.getcwd(), ['.h'])
for h in root_dir_headers:
fix_headers(h)
for dir in lang_dirs:
flatten_dir(dir)
for f in ls_exts(dir, ['.h', '.c']):
fix_headers(f)
55 changes: 55 additions & 0 deletions kotlin/alloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@


////////////////////////////////////////////////////////////////////////////////
// Poolside modification in the fork, generated at 2024-09-06 11:07:56.663188 //
////////////////////////////////////////////////////////////////////////////////


#ifndef TREE_SITTER_ALLOC_H_
#define TREE_SITTER_ALLOC_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#if defined(TREE_SITTER_HIDDEN_SYMBOLS) || defined(_WIN32)
#define TS_PUBLIC
#else
#define TS_PUBLIC __attribute__((visibility("default")))
#endif

TS_PUBLIC extern void *(*ts_current_malloc)(size_t);
TS_PUBLIC extern void *(*ts_current_calloc)(size_t, size_t);
TS_PUBLIC extern void *(*ts_current_realloc)(void *, size_t);
TS_PUBLIC extern void (*ts_current_free)(void *);

// Allow clients to override allocation functions
#ifndef ts_malloc
#define ts_malloc ts_current_malloc
#endif
#ifndef ts_calloc
#define ts_calloc ts_current_calloc
#endif
#ifndef ts_realloc
#define ts_realloc ts_current_realloc
#endif
#ifndef ts_free
#define ts_free ts_current_free
#endif

#ifdef __cplusplus
}
#endif

#endif // TREE_SITTER_ALLOC_H_


////////////////////////////////////////////////////////////////////////////////
// Poolside modification in the fork, generated at 2024-09-06 11:07:56.663188 //
////////////////////////////////////////////////////////////////////////////////


Loading

0 comments on commit 85a8095

Please sign in to comment.