Skip to content

Commit

Permalink
[refactor] Reformat with clang-format and get it to compile again.
Browse files Browse the repository at this point in the history
The #include sorting exposed some issues.

Side effect: we have ~30% fewer lines of preprocessed code!  Got rid of
some #includes in mylib.h.

See https://oilshell.zulipchat.com/#narrow/stream/121539-oil-dev/topic/New.20preprocessed.20metric

The version of Clang and clang-format we use is in soil/deps-binary.sh.
  • Loading branch information
Andy C committed Jun 27, 2022
1 parent 8c59ddb commit 4111cd2
Show file tree
Hide file tree
Showing 32 changed files with 225 additions and 230 deletions.
5 changes: 2 additions & 3 deletions asdl/gc_test.cc
Expand Up @@ -4,15 +4,14 @@
#include "asdl/runtime.gc.h"
#include "mycpp/gc_heap.h"
#include "mycpp/mylib2.h"
#include "vendor/greatest.h"

#include "typed_demo_asdl.gc.h"
#include "vendor/greatest.h"

using gc_heap::Alloc;
using gc_heap::NewStr;

using hnode_asdl::hnode_t;
using hnode_asdl::hnode_str;
using hnode_asdl::hnode_t;

using typed_demo_asdl::bool_expr__Binary;
using typed_demo_asdl::word;
Expand Down
3 changes: 1 addition & 2 deletions asdl/gen_cpp_test.cc
Expand Up @@ -3,10 +3,9 @@

#include "asdl/runtime.h"
#include "mycpp/mylib.h"
#include "vendor/greatest.h"

#include "typed_arith_asdl.h"
#include "typed_demo_asdl.h" // has simple Sum, etc
#include "vendor/greatest.h"

using typed_arith_asdl::pipeline;

Expand Down
1 change: 1 addition & 0 deletions build/native_graph.py
Expand Up @@ -73,6 +73,7 @@ def log(msg, *args):
'cpp/osh_arith_parse.cc',
'cpp/osh_bool_stat.cc',
'cpp/pgen2_parse.cc',
'cpp/pylib_path_stat.cc',
'cpp/pylib_os_path.cc',
'cpp/dumb_alloc.cc',
'cpp/fcntl_.cc',
Expand Down
3 changes: 1 addition & 2 deletions cpp/core_error.h
Expand Up @@ -147,8 +147,7 @@ class ErrExit : public _ErrorWithLocation {
// Stub: the parts that raise aren't translated
class Expr : public _ErrorWithLocation {
public:
Expr(Str* user_str, int span_id)
: _ErrorWithLocation(user_str, span_id) {
Expr(Str* user_str, int span_id) : _ErrorWithLocation(user_str, span_id) {
}
#if 0
Expr(Str* user_str, Token* token)
Expand Down
1 change: 1 addition & 0 deletions cpp/core_pyos.cc
@@ -1,6 +1,7 @@
// core_pyos.cc

#include "core_pyos.h" // undefined errno

#include <errno.h>
#include <pwd.h>
#include <signal.h>
Expand Down
2 changes: 1 addition & 1 deletion cpp/dumb_alloc.cc
Expand Up @@ -27,7 +27,7 @@ inline size_t aligned(size_t n) {
return (n + kMask) & ~kMask;
}

// This global interface is silly ...
// This global interface is silly ...

#ifdef DUMB_ALLOC
void* operator new(size_t size) {
Expand Down
4 changes: 2 additions & 2 deletions cpp/fcntl_.cc
Expand Up @@ -4,11 +4,11 @@
#include "mycpp/myerror.h" // for IOError
// clang-format on

#include "fcntl_.h"

#include <errno.h>
#include <fcntl.h> // the system header

#include "fcntl_.h"

namespace fcntl_ {

int fcntl(int fd, int cmd) {
Expand Down
3 changes: 1 addition & 2 deletions cpp/frontend_match.h
Expand Up @@ -3,9 +3,8 @@
#ifndef MATCH_H
#define MATCH_H

#include "mycpp/mylib.h"

#include "_build/cpp/id_kind_asdl.h" // syntax.asdl depends on this
#include "mycpp/mylib.h"
using id_kind_asdl::Id_t; // TODO: proper ASDL modules

#include "_build/cpp/syntax_asdl.h"
Expand Down
1 change: 1 addition & 0 deletions cpp/libc.cc
@@ -1,6 +1,7 @@
// libc.cc: Replacement for native/libcmodule.c

#include "libc.h"

#include <glob.h>
#include <locale.h>
#include <regex.h>
Expand Down
129 changes: 128 additions & 1 deletion cpp/osh_bool_stat.cc
@@ -1,10 +1,13 @@
// osh_bool_stat.cc

#include "osh_bool_stat.h"

#include <fcntl.h> // AT_* Constants
#include <sys/stat.h>
#include <unistd.h>

#include "core_error.h"
#include "core_pyerror.h"
#include "osh_bool_stat.h"

namespace bool_stat {

Expand All @@ -21,4 +24,128 @@ bool isatty(Str* fd_str, word_t* blame_word) {
return result;
}

bool DoUnaryOp(Id_t op_id, Str* s) {
mylib::Str0 path(s);

const char* zPath = path.Get();

if (op_id == Id::BoolUnary_h || op_id == Id::BoolUnary_L) {
struct stat st;
if (lstat(zPath, &st) < 0) {
return false;
}

return S_ISLNK(st.st_mode);
} else {
struct stat st;
if (stat(zPath, &st) < 0) {
return false;
}

auto mode = st.st_mode;

switch (op_id) {
// synonyms for existence
case Id::BoolUnary_a:
case Id::BoolUnary_e:
return true;

case Id::BoolUnary_s:
return st.st_size != 0;

case Id::BoolUnary_d:
return S_ISDIR(mode);

case Id::BoolUnary_f:
return S_ISREG(mode);

case Id::BoolUnary_k:
return (mode & S_ISVTX) != 0;

case Id::BoolUnary_p:
return S_ISFIFO(mode);

case Id::BoolUnary_O:
return st.st_uid == geteuid();

case Id::BoolUnary_G:
return st.st_gid == getegid();

case Id::BoolUnary_u:
return mode & S_ISUID;

case Id::BoolUnary_g:
return mode & S_ISGID;

// NOTE(Jesse): This implementation MAY have a bug. On my system (Ubuntu
// 20.04) it returns a correct result if the user is root (elevated with
// sudo) and no execute bits are set for a file.
//
// A bug worked around in the python `posix` module here is that the above
// (working) scenario is not always the case.
//
// https://github.com/python/cpython/blob/8d999cbf4adea053be6dbb612b9844635c4dfb8e/Modules/posixmodule.c#L2547
//
// As well as the dash source code found here (relative to this repo
// root):
//
// _cache/spec-bin/dash-0.5.10.2/src/bltin/test.c
// See `test_file_access()`
//
// We could also use the `stat` struct to manually compute the
// permissions, as shown in the above `test.c`, though the code is
// somewhat obtuse.
//
// There is further discussion of this issue in:
// https://github.com/oilshell/oil/pull/1168
//
// And a bug filed for it at:
//
// https://github.com/oilshell/oil/issues/1170
//
case Id::BoolUnary_x:
return faccessat(AT_FDCWD, zPath, X_OK, AT_EACCESS) == 0;
//

case Id::BoolUnary_r:
return faccessat(AT_FDCWD, zPath, R_OK, AT_EACCESS) == 0;

case Id::BoolUnary_w:
return faccessat(AT_FDCWD, zPath, W_OK, AT_EACCESS) == 0;
}
}

assert(0);

return false;
}

bool DoBinaryOp(Id_t op_id, Str* s1, Str* s2) {
mylib::Str0 left0(s1);
mylib::Str0 right0(s2);

int m1 = 0;
struct stat st1;
if (stat(left0.Get(), &st1) == 0) {
m1 = st1.st_mtime;
}

int m2 = 0;
struct stat st2;
if (stat(right0.Get(), &st2) == 0) {
m2 = st2.st_mtime;
}

switch (op_id) {
case Id::BoolBinary_nt:
return m1 > m2;
case Id::BoolBinary_ot:
return m1 < m2;
case Id::BoolBinary_ef:
return st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino;
}

assert(0);
}

} // namespace bool_stat
133 changes: 2 additions & 131 deletions cpp/osh_bool_stat.h
Expand Up @@ -3,9 +3,6 @@
#ifndef OSH_BOOL_STAT_H
#define OSH_BOOL_STAT_H

#include <sys/stat.h>
#include <fcntl.h> // AT_* Constants

#include "_build/cpp/syntax_asdl.h"
#include "mycpp/mylib.h"

Expand All @@ -15,134 +12,8 @@ namespace Id = id_kind_asdl::Id;
using syntax_asdl::word_t;

bool isatty(Str* fd_str, word_t* blame_word);

inline bool DoUnaryOp(Id_t op_id, Str* s) {
mylib::Str0 path(s);

const char *zPath = path.Get();

if (op_id == Id::BoolUnary_h ||
op_id == Id::BoolUnary_L )
{
struct stat st;
if (lstat(zPath, &st) < 0) {
return false;
}

return S_ISLNK(st.st_mode);
}
else
{
struct stat st;
if (stat(zPath, &st) < 0) {
return false;
}

auto mode = st.st_mode;

switch (op_id) {
// synonyms for existence
case Id::BoolUnary_a:
case Id::BoolUnary_e:
return true;

case Id::BoolUnary_s:
return st.st_size != 0;

case Id::BoolUnary_d:
return S_ISDIR(mode);

case Id::BoolUnary_f:
return S_ISREG(mode);

case Id::BoolUnary_k:
return (mode & S_ISVTX) != 0;

case Id::BoolUnary_p:
return S_ISFIFO(mode);

case Id::BoolUnary_O:
return st.st_uid == geteuid();

case Id::BoolUnary_G:
return st.st_gid == getegid();

case Id::BoolUnary_u:
return mode & S_ISUID;

case Id::BoolUnary_g:
return mode & S_ISGID;


// NOTE(Jesse): This implementation MAY have a bug. On my system (Ubuntu
// 20.04) it returns a correct result if the user is root (elevated with
// sudo) and no execute bits are set for a file.
//
// A bug worked around in the python `posix` module here is that the above
// (working) scenario is not always the case.
//
// https://github.com/python/cpython/blob/8d999cbf4adea053be6dbb612b9844635c4dfb8e/Modules/posixmodule.c#L2547
//
// As well as the dash source code found here (relative to this repo root):
//
// _cache/spec-bin/dash-0.5.10.2/src/bltin/test.c
// See `test_file_access()`
//
// We could also use the `stat` struct to manually compute the permissions,
// as shown in the above `test.c`, though the code is somewhat obtuse.
//
// There is further discussion of this issue in:
// https://github.com/oilshell/oil/pull/1168
//
// And a bug filed for it at:
//
// https://github.com/oilshell/oil/issues/1170
//
case Id::BoolUnary_x:
return faccessat(AT_FDCWD, zPath, X_OK, AT_EACCESS) == 0;
//

case Id::BoolUnary_r:
return faccessat(AT_FDCWD, zPath, R_OK, AT_EACCESS) == 0;

case Id::BoolUnary_w:
return faccessat(AT_FDCWD, zPath, W_OK, AT_EACCESS) == 0;
}
}


assert(0);

return false;
}

inline bool DoBinaryOp(Id_t op_id, Str* s1, Str* s2) {
mylib::Str0 left0(s1);
mylib::Str0 right0(s2);

int m1 = 0;
struct stat st1;
if (stat(left0.Get(), &st1) == 0) {
m1 = st1.st_mtime;
}

int m2 = 0;
struct stat st2;
if (stat(right0.Get(), &st2) == 0) {
m2 = st2.st_mtime;
}

switch (op_id) {
case Id::BoolBinary_nt:
return m1 > m2;
case Id::BoolBinary_ot:
return m1 < m2;
case Id::BoolBinary_ef:
return st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino;
}

assert(0);
}
bool DoUnaryOp(Id_t op_id, Str* s);
bool DoBinaryOp(Id_t op_id, Str* s1, Str* s2);

} // namespace bool_stat

Expand Down
1 change: 1 addition & 0 deletions cpp/posix.cc
Expand Up @@ -6,6 +6,7 @@

#include "posix.h"

#include <errno.h>
#include <fcntl.h> // open
#include <sys/wait.h> // WUNTRACED
#include <unistd.h>
Expand Down

0 comments on commit 4111cd2

Please sign in to comment.