Skip to content

Commit

Permalink
Add some clang scripts; add ChangeLog function; update clang_format
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinushey committed Feb 19, 2014
1 parent 5ec0d1d commit 83532a9
Show file tree
Hide file tree
Showing 10 changed files with 244 additions and 68 deletions.
24 changes: 12 additions & 12 deletions .Rbuildignore
@@ -1,19 +1,19 @@
^.*\.Rproj$
^\.Rproj\.user$
.DS_Store
benchmark/
^benchmark/
Rplots.pdf
test/
src-i386/
src-x86_64/
stress/
incubator/
old/
.idea
dist/
nbproject/
bench/
Kmisc/
^test/
^src-i386/
^src-x86_64/
^stress/
^incubator/
^old/
^.idea
^dist/
^nbproject/
^bench/
^Kmisc/
Makefile
README.md
vignettes/*.md
Expand Down
1 change: 1 addition & 0 deletions DESCRIPTION
Expand Up @@ -31,6 +31,7 @@ Suggests:
ByteCompile: TRUE
VignetteBuilder: knitr
Collate:
'ChangeLog.R'
'Kmisc-md2html.R'
'Kmisc-package.R'
'Rcpp.gallery.skeleton.R'
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Expand Up @@ -10,6 +10,7 @@ S3method(transpose,list)
export("%kin%")
export("%knin%")
export("%nin%")
export(ChangeLog)
export(Kmisc.knit2html)
export(Rcpp_apply_generator)
export(Rcpp_tapply_generator)
Expand Down
59 changes: 59 additions & 0 deletions R/ChangeLog.R
@@ -0,0 +1,59 @@
##' Update a Package ChangeLog
##'
##' This function compares the time that files were last modified with the
##' time that the current \R session was started (well, the time that
##' \code{Kmisc} was loaded), generates stubs for them in a file called
##' \code{ChangeLog}, and then opens that file in an editor.
##' @param msg A message to use for the first file.
##' @export
ChangeLog <- function(msg="") {

if (!exists(".Start.time")) {
stop("No variable '.Start.time' available; unable to infer what ",
"files have been modified in the current session!")
}

if (!file.exists("ChangeLog")) {
message("Creating a new ChangeLog file...")
file.create("ChangeLog")
}

## Find out what files have been modified in the current session
files <- list.files(full.names=TRUE, include.dirs=FALSE)

times <- unlist( lapply(files, function(x) {
as.numeric(system(paste("stat -f%c", x), intern=TRUE))
}))

modified <- files[ times > .Start.time ]

## The ChangeLog is not a candidate for listing in modifies
modified <- modified[ modified != "./ChangeLog" ]

## Strip off the initial './'
modified <- substring(modified, 3, nchar(modified))

if (length(modified)) {

## Generate the header
header <- paste0( Sys.Date(), " ", Sys.getenv("USERNAME"), " <", Sys.getenv("EMAIL"), ">" )

## Stubs for each modified file
body_first <- paste0(" * ", modified[1], ": ", msg)
body_rest <- paste0(" * ", modified[2:length(modified)], ": Idem")

changes <- c(header, "", body_first, body_rest)

## Read in the ChangeLog, put the new changes on top, and write it back out
ChangeLog <- readLines("ChangeLog")
New <- c(changes, ChangeLog)

## Write out the new ChangeLog, and open it in an editor
cat(New, file="ChangeLog", sep="\n")
return( file.edit("ChangeLog") )
} else {
message("No files have been modified in the current session.")
return (invisible(NULL))
}

}
2 changes: 1 addition & 1 deletion R/clang_format.R
@@ -1,7 +1,7 @@
##' Reformat C/C++ Code using clang-format
##'
##' This function requires \code{clang-format} to be in your \code{PATH}.
##'
##' @rdname clang
##' @export
clang_format <- function(
files=NULL,
Expand Down
17 changes: 17 additions & 0 deletions man/ChangeLog.Rd
@@ -0,0 +1,17 @@
\name{ChangeLog}
\alias{ChangeLog}
\title{Update a Package ChangeLog}
\usage{
ChangeLog(msg = "")
}
\arguments{
\item{msg}{A message to use for the first file.}
}
\description{
This function compares the time that files were last
modified with the time that the current \R session was
started (well, the time that \code{Kmisc} was loaded),
generates stubs for them in a file called \code{ChangeLog},
and then opens that file in an editor.
}

12 changes: 12 additions & 0 deletions man/clang.Rd
@@ -0,0 +1,12 @@
\name{clang_format}
\alias{clang_format}
\title{Reformat C/C++ Code using clang-format}
\usage{
clang_format(files = NULL, style = c("Chromium", "LLVM", "Google",
"Mozilla", "WebKit"), verbose = FALSE)
}
\description{
This function requires \code{clang-format} to be in your
\code{PATH}.
}

144 changes: 89 additions & 55 deletions src/Kmisc_init.c
Expand Up @@ -5,87 +5,121 @@

#include <R_ext/Rdynload.h>

SEXP any_na( SEXP x );
SEXP charlist_transpose_to_df( SEXP x, SEXP names );
SEXP any_na(SEXP x);
SEXP charlist_transpose_to_df(SEXP x, SEXP names);
SEXP copy(SEXP x_);
SEXP df2list(SEXP x_, SEXP inplace);
SEXP double2hex(SEXP x);
SEXP int2hex(SEXP x);
SEXP enumerate(SEXP call, SEXP env, SEXP nargs_);
SEXP factor_to_char( SEXP X_, SEXP inplace_ );
SEXP in_interval( SEXP x, SEXP lo, SEXP hi, SEXP include_lower, SEXP include_upper );
SEXP factor_to_char(SEXP X_, SEXP inplace_);
SEXP in_interval(SEXP x,
SEXP lo,
SEXP hi,
SEXP include_lower,
SEXP include_upper);
SEXP list2df(SEXP x_, SEXP inplace);
SEXP list2mat(SEXP x);
SEXP mat2df(SEXP x);
SEXP mat2list(SEXP x);
SEXP melt_dataframe( SEXP x, SEXP id_ind_, SEXP val_ind_, SEXP variable_name, SEXP value_name );
SEXP melt_matrix( SEXP x );
SEXP melt_dataframe(SEXP x,
SEXP id_ind_,
SEXP val_ind_,
SEXP variable_name,
SEXP value_name);
SEXP melt_matrix(SEXP x);
SEXP setnamed(SEXP x, SEXP i);
SEXP simp( SEXP x, SEXP y );
SEXP str_rev( SEXP x );
SEXP simp(SEXP x, SEXP y);
SEXP str_rev(SEXP x);
SEXP str_slice(SEXP x, SEXP n);
SEXP transpose_list(SEXP x_);
SEXP unmelt(SEXP data, SEXP uniq_id, SEXP other_ind_, SEXP id_ind_, SEXP value_ind_);
SEXP unmelt(SEXP data,
SEXP uniq_id,
SEXP other_ind_,
SEXP id_ind_,
SEXP value_ind_);
SEXP Kmisc_char_to_factor(SEXP x_SEXP, SEXP inplaceSEXP);
SEXP Kmisc_tableRcpp(SEXP xSEXP);
SEXP Kmisc_counts(SEXP xSEXP);
SEXP Kmisc_extract_rows_from_file_to_file(SEXP input_file_nameSEXP, SEXP output_file_nameSEXP, SEXP delimSEXP, SEXP items_to_keepSEXP, SEXP column_to_checkSEXP);
SEXP Kmisc_extract_rows_from_file(SEXP input_file_nameSEXP, SEXP delimSEXP, SEXP items_to_keepSEXP, SEXP column_to_checkSEXP);
SEXP Kmisc_extract_rows_from_file_to_file(SEXP input_file_nameSEXP,
SEXP output_file_nameSEXP,
SEXP delimSEXP,
SEXP items_to_keepSEXP,
SEXP column_to_checkSEXP);
SEXP Kmisc_extract_rows_from_file(SEXP input_file_nameSEXP,
SEXP delimSEXP,
SEXP items_to_keepSEXP,
SEXP column_to_checkSEXP);
SEXP Kmisc_fast_factor(SEXP xSEXP, SEXP levelsSEXP);
SEXP Kmisc_matches(SEXP xSEXP);
SEXP Kmisc_read(SEXP pathSEXP, SEXP linesSEXP);
SEXP Kmisc_split_file(SEXP pathSEXP, SEXP dirSEXP, SEXP basenameSEXP, SEXP path_sepSEXP, SEXP sepSEXP, SEXP prependSEXP, SEXP file_extSEXP, SEXP columnSEXP, SEXP skipSEXP, SEXP verboseSEXP);
SEXP Kmisc_split_file(SEXP pathSEXP,
SEXP dirSEXP,
SEXP basenameSEXP,
SEXP path_sepSEXP,
SEXP sepSEXP,
SEXP prependSEXP,
SEXP file_extSEXP,
SEXP columnSEXP,
SEXP skipSEXP,
SEXP verboseSEXP);
SEXP Kmisc_split_runs_numeric(SEXP XSEXP);
SEXP Kmisc_split_runs_character(SEXP XSEXP);
SEXP Kmisc_split_runs_one(SEXP xSEXP);
SEXP Kmisc_split(SEXP xSEXP, SEXP ySEXP);
SEXP Kmisc_stack_list_df(SEXP XSEXP, SEXP classesSEXP, SEXP num_elemSEXP, SEXP make_row_namesSEXP, SEXP nameSEXP, SEXP keep_list_indexSEXP, SEXP index_nameSEXP);
SEXP Kmisc_stack_list_df(SEXP XSEXP,
SEXP classesSEXP,
SEXP num_elemSEXP,
SEXP make_row_namesSEXP,
SEXP nameSEXP,
SEXP keep_list_indexSEXP,
SEXP index_nameSEXP);
SEXP Kmisc_str_collapse_list(SEXP xSEXP);
SEXP Kmisc_str_collapse_str(SEXP xSEXP);

R_CallMethodDef callMethods[] = {
{"Cany_na", (DL_FUNC) &any_na, 1},
{"Ccharlist_transpose_to_df", (DL_FUNC) &charlist_transpose_to_df, 2},
{"Ccopy", (DL_FUNC) &copy, 1},
{"Cdf2list", (DL_FUNC) &df2list, 2},
{"Cdouble2hex", (DL_FUNC) &double2hex, 1},
{"Cint2hex", (DL_FUNC) &int2hex, 1},
{"Cenumerate", (DL_FUNC) &enumerate, 3},
{"Cfactor_to_char", (DL_FUNC) &factor_to_char, 2},
{"Cin_interval", (DL_FUNC) &in_interval, 5},
{"Clist2df", (DL_FUNC) &list2df, 2},
{"Clist2mat", (DL_FUNC) &list2mat, 1},
{"Cmat2df", (DL_FUNC) &mat2df, 1},
{"Cmat2list", (DL_FUNC) &mat2list, 1},
{"Cmelt_dataframe", (DL_FUNC) &melt_dataframe, 5},
{"Cmelt_matrix", (DL_FUNC) &melt_matrix, 1},
{"Csetnamed", (DL_FUNC) &setnamed, 2},
{"Csimp", (DL_FUNC) &simp, 2},
{"Cstr_rev", (DL_FUNC) &str_rev, 1},
{"Cstr_slice", (DL_FUNC) &str_slice, 2},
{"Ctranspose_list", (DL_FUNC) &transpose_list, 1},
{"Cunmelt", (DL_FUNC) &unmelt, 5},
{"CKmisc_char_to_factor", (DL_FUNC) &Kmisc_char_to_factor, 2},
{"CKmisc_tableRcpp", (DL_FUNC) &Kmisc_tableRcpp, 1},
{"CKmisc_counts", (DL_FUNC) &Kmisc_counts, 1},
{"CKmisc_extract_rows_from_file_to_file", (DL_FUNC) &Kmisc_extract_rows_from_file_to_file, 5},
{"CKmisc_extract_rows_from_file", (DL_FUNC) &Kmisc_extract_rows_from_file, 4},
{"CKmisc_fast_factor", (DL_FUNC) &Kmisc_fast_factor, 2},
{"CKmisc_matches", (DL_FUNC) &Kmisc_matches, 1},
{"CKmisc_read", (DL_FUNC) &Kmisc_read, 2},
{"CKmisc_split_file", (DL_FUNC) &Kmisc_split_file, 10},
{"CKmisc_split_runs_numeric", (DL_FUNC) &Kmisc_split_runs_numeric, 1},
{"CKmisc_split_runs_character", (DL_FUNC) &Kmisc_split_runs_character, 1},
{"CKmisc_split_runs_one", (DL_FUNC) &Kmisc_split_runs_one, 1},
{"CKmisc_split", (DL_FUNC) &Kmisc_split, 2},
{"CKmisc_stack_list_df", (DL_FUNC) &Kmisc_stack_list_df, 7},
{"CKmisc_str_collapse_list", (DL_FUNC) &Kmisc_str_collapse_list, 1},
{"CKmisc_str_collapse_str", (DL_FUNC) &Kmisc_str_collapse_str, 1},
{NULL, NULL, 0}
};
R_CallMethodDef callMethods[] = {
{"Cany_na", (DL_FUNC) & any_na, 1},
{"Ccharlist_transpose_to_df", (DL_FUNC) & charlist_transpose_to_df, 2},
{"Ccopy", (DL_FUNC) & copy, 1},
{"Cdf2list", (DL_FUNC) & df2list, 2},
{"Cdouble2hex", (DL_FUNC) & double2hex, 1},
{"Cint2hex", (DL_FUNC) & int2hex, 1},
{"Cenumerate", (DL_FUNC) & enumerate, 3},
{"Cfactor_to_char", (DL_FUNC) & factor_to_char, 2},
{"Cin_interval", (DL_FUNC) & in_interval, 5},
{"Clist2df", (DL_FUNC) & list2df, 2},
{"Clist2mat", (DL_FUNC) & list2mat, 1},
{"Cmat2df", (DL_FUNC) & mat2df, 1},
{"Cmat2list", (DL_FUNC) & mat2list, 1},
{"Cmelt_dataframe", (DL_FUNC) & melt_dataframe, 5},
{"Cmelt_matrix", (DL_FUNC) & melt_matrix, 1},
{"Csetnamed", (DL_FUNC) & setnamed, 2},
{"Csimp", (DL_FUNC) & simp, 2},
{"Cstr_rev", (DL_FUNC) & str_rev, 1},
{"Cstr_slice", (DL_FUNC) & str_slice, 2},
{"Ctranspose_list", (DL_FUNC) & transpose_list, 1},
{"Cunmelt", (DL_FUNC) & unmelt, 5},
{"CKmisc_char_to_factor", (DL_FUNC) & Kmisc_char_to_factor, 2},
{"CKmisc_tableRcpp", (DL_FUNC) & Kmisc_tableRcpp, 1},
{"CKmisc_counts", (DL_FUNC) & Kmisc_counts, 1},
{"CKmisc_extract_rows_from_file_to_file",
(DL_FUNC) & Kmisc_extract_rows_from_file_to_file, 5},
{"CKmisc_extract_rows_from_file", (DL_FUNC) & Kmisc_extract_rows_from_file,
4},
{"CKmisc_fast_factor", (DL_FUNC) & Kmisc_fast_factor, 2},
{"CKmisc_matches", (DL_FUNC) & Kmisc_matches, 1},
{"CKmisc_read", (DL_FUNC) & Kmisc_read, 2},
{"CKmisc_split_file", (DL_FUNC) & Kmisc_split_file, 10},
{"CKmisc_split_runs_numeric", (DL_FUNC) & Kmisc_split_runs_numeric, 1},
{"CKmisc_split_runs_character", (DL_FUNC) & Kmisc_split_runs_character, 1},
{"CKmisc_split_runs_one", (DL_FUNC) & Kmisc_split_runs_one, 1},
{"CKmisc_split", (DL_FUNC) & Kmisc_split, 2},
{"CKmisc_stack_list_df", (DL_FUNC) & Kmisc_stack_list_df, 7},
{"CKmisc_str_collapse_list", (DL_FUNC) & Kmisc_str_collapse_list, 1},
{"CKmisc_str_collapse_str", (DL_FUNC) & Kmisc_str_collapse_str, 1},
{NULL, NULL, 0}};

void R_init_Kmisc(DllInfo *info) {
void R_init_Kmisc(DllInfo* info) {
R_registerRoutines(info, NULL, callMethods, NULL, NULL);
R_useDynamicSymbols(info, FALSE);
}

23 changes: 23 additions & 0 deletions tools/check.sh
@@ -0,0 +1,23 @@
#!/usr/bin/env sh

IN_KMISC=`pwd | grep Kmisc$`
if [ ${#IN_KMISC} == 0 ]; then
echo "Error: not in base Kmisc directory."
exit 0
fi;

## NOTE: For some reason, clang-modernize tries to add a second
## set of overrides to Rstreambuf.h, so we exclude it explicitly

## I also currently get assertion errors if I try to use
## '-use-auto', hence I explicitly set some parameters for modernizing

clang-check src/*.cpp \
-analyze \
-- \
-std=c++1y \
-I/Users/kevinushey/.llvm/libcxx/include \
-I/Library/Frameworks/R.framework/Headers \
-I/Library/Frameworks/R.framework/Resources/library/Rcpp/include \
-Iinst/include

29 changes: 29 additions & 0 deletions tools/modernize.sh
@@ -0,0 +1,29 @@
#!/usr/bin/env sh

IN_KMISC=`pwd | grep Kmisc$`
if [ ${#IN_KMISC} == 0 ]; then
echo "Error: not in base Kmisc directory."
exit 0
fi;

## NOTE: For some reason, clang-modernize tries to add a second
## set of overrides to Rstreambuf.h, so we exclude it explicitly

## I also currently get assertion errors if I try to use
## '-use-auto', hence I explicitly set some parameters for modernizing

clang-modernize src/*.cpp \
-final-syntax-check \
-style="Chromium" \
-risk=risky \
-pass-by-value \
-loop-convert \
-add-override \
-for-compilers=clang-3.5 \
-- \
-std=c++1y \
-I/Users/kevinushey/.llvm/libcxx/include \
-I/Library/Frameworks/R.framework/Headers \
-I/Library/Frameworks/R.framework/Resources/library/Rcpp/include \
-Iinst/include

0 comments on commit 83532a9

Please sign in to comment.