aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2009-06-16 12:03:32 -0700
committerRobert Griesemer <gri@golang.org>2009-06-16 12:03:32 -0700
commitd8e4446d129f823a3f92d5750b1bf8fdd1d1a242 (patch)
treebf08ca44cdd180a7538a713d1a55e2925a83ac1c /src
parent1ac2cfc72042ffbe17155b9407907d6686dc4664 (diff)
downloadgo-d8e4446d129f823a3f92d5750b1bf8fdd1d1a242.tar.xz
- install gofmt in src/cmd/gofmt
- remove some left-over files R=rsc DELTA=1465 (281 added, 1181 deleted, 3 changed) OCL=30350 CL=30353
Diffstat (limited to 'src')
-rw-r--r--src/cmd/gofmt/Makefile27
-rw-r--r--src/cmd/gofmt/gofmt.go107
-rwxr-xr-xsrc/cmd/gofmt/test.sh152
-rwxr-xr-xsrc/make.bash2
-rwxr-xr-xsrc/run.bash6
5 files changed, 289 insertions, 5 deletions
diff --git a/src/cmd/gofmt/Makefile b/src/cmd/gofmt/Makefile
new file mode 100644
index 0000000000..1312cb19d5
--- /dev/null
+++ b/src/cmd/gofmt/Makefile
@@ -0,0 +1,27 @@
+# Copyright 2009 The Go Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style
+# license that can be found in the LICENSE file.
+
+include $(GOROOT)/src/Make.$(GOARCH)
+
+TARG=gofmt
+OFILES=\
+ gofmt.$O\
+
+$(TARG): $(OFILES)
+ $(LD) -o $(TARG) $(OFILES)
+
+test: $(TARG)
+ ./test.sh
+
+smoketest: $(TARG)
+ ./test.sh $(GOROOT)/src/pkg/go/parser/parser.go
+
+clean:
+ rm -f $(OFILES) $(TARG)
+
+install: $(TARG)
+ cp $(TARG) $(HOME)/bin/$(TARG)
+
+%.$O: %.go
+ $(GC) $<
diff --git a/src/cmd/gofmt/gofmt.go b/src/cmd/gofmt/gofmt.go
new file mode 100644
index 0000000000..73f9d8e23c
--- /dev/null
+++ b/src/cmd/gofmt/gofmt.go
@@ -0,0 +1,107 @@
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+import (
+ "flag";
+ "fmt";
+ "go/parser";
+ "go/printer";
+ "io";
+ "os";
+ "sort";
+ "tabwriter";
+)
+
+
+var (
+ // operation modes
+ silent = flag.Bool("s", false, "silent mode: parsing only");
+ verbose = flag.Bool("v", false, "verbose mode: trace parsing");
+ exports = flag.Bool("x", false, "show exports only");
+
+ // layout control
+ tabwidth = flag.Int("tabwidth", 4, "tab width");
+ usetabs = flag.Bool("tabs", false, "align with tabs instead of blanks");
+ optcommas = flag.Bool("optcommas", false, "print optional commas");
+ optsemis = flag.Bool("optsemis", false, "print optional semicolons");
+)
+
+
+func usage() {
+ fmt.Fprintf(os.Stderr, "usage: gofmt [flags] [file.go]\n");
+ flag.PrintDefaults();
+ os.Exit(1);
+}
+
+
+func parserMode() uint {
+ mode := parser.ParseComments;
+ if *verbose {
+ mode |= parser.Trace;
+ }
+ return mode;
+}
+
+
+func printerMode() uint {
+ mode := uint(0);
+ if *exports {
+ mode |= printer.ExportsOnly;
+ }
+ if *optcommas {
+ mode |= printer.OptCommas;
+ }
+ if *optsemis {
+ mode |= printer.OptSemis;
+ }
+ return mode;
+}
+
+
+func makeTabwriter(writer io.Writer) *tabwriter.Writer {
+ padchar := byte(' ');
+ if *usetabs {
+ padchar = '\t';
+ }
+ return tabwriter.NewWriter(writer, *tabwidth, 1, padchar, 0);
+}
+
+
+func main() {
+ flag.Parse();
+
+ var filename string;
+ switch flag.NArg() {
+ case 0: filename = "/dev/stdin";
+ case 1: filename = flag.Arg(0);
+ default: usage();
+ }
+
+ src, err := io.ReadFile(filename);
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "%s: %v\n", filename, err);
+ os.Exit(1);
+ }
+
+ prog, err := parser.Parse(src, parserMode());
+ if err != nil {
+ if errors, ok := err.(parser.ErrorList); ok {
+ sort.Sort(errors);
+ for _, e := range errors {
+ fmt.Fprintf(os.Stderr, "%s:%v\n", filename, e);
+ }
+ } else {
+ fmt.Fprintf(os.Stderr, "%s: %v\n", filename, err);
+ }
+ os.Exit(1);
+ }
+
+ if !*silent {
+ w := makeTabwriter(os.Stdout);
+ printer.Fprint(w, prog, printerMode());
+ w.Flush();
+ }
+}
diff --git a/src/cmd/gofmt/test.sh b/src/cmd/gofmt/test.sh
new file mode 100755
index 0000000000..25734af058
--- /dev/null
+++ b/src/cmd/gofmt/test.sh
@@ -0,0 +1,152 @@
+#!/bin/bash
+# Copyright 2009 The Go Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style
+# license that can be found in the LICENSE file.
+
+. $GOROOT/src/Make.$GOARCH
+if [ -z "$O" ]; then
+ echo 'missing $O - maybe no Make.$GOARCH?' 1>&2
+ exit 1
+fi
+
+CMD="./gofmt"
+TMP1=test_tmp1.go
+TMP2=test_tmp2.go
+TMP3=test_tmp3.go
+COUNT=0
+
+count() {
+ #echo $1
+ let COUNT=$COUNT+1
+ let M=$COUNT%10
+ if [ $M == 0 ]; then
+ echo -n "."
+ fi
+}
+
+
+# apply to one file
+apply1() {
+ #echo $1 $2
+ case `basename $F` in
+ # files with errors (skip them)
+ # the following have semantic errors: bug039.go | bug040.go
+ test_errors.go | calc.go | method1.go | selftest1.go | func3.go | const2.go | \
+ bug014.go | bug025.go | bug029.go | bug032.go | bug039.go | bug040.go | bug050.go | bug068.go | \
+ bug088.go | bug083.go | bug106.go | bug121.go | bug125.go | bug126.go | bug132.go | bug133.go | bug134.go | bug160.go ) ;;
+ * ) $1 $2; count $F;;
+ esac
+}
+
+
+# apply to local files
+applydot() {
+ for F in `find . -name "*.go" | grep -v "OLD" | grep -v "._"`; do
+ apply1 $1 $F
+ done
+}
+
+
+# apply to all .go files we can find
+apply() {
+ for F in `find $GOROOT -name "*.go" | grep -v "OLD" | grep -v "._"`; do
+ apply1 $1 $F
+ done
+}
+
+
+cleanup() {
+ rm -f $TMP1 $TMP2 $TMP3
+}
+
+
+silent() {
+ cleanup
+ $CMD -s $1 > $TMP1
+ if [ $? != 0 ]; then
+ cat $TMP1
+ echo "Error (silent mode test): test.sh $1"
+ exit 1
+ fi
+}
+
+
+idempotent() {
+ cleanup
+ $CMD $1 > $TMP1
+ if [ $? != 0 ]; then
+ echo "Error (step 1 of idempotency test): test.sh $1"
+ exit 1
+ fi
+
+ $CMD $TMP1 > $TMP2
+ if [ $? != 0 ]; then
+ echo "Error (step 2 of idempotency test): test.sh $1"
+ exit 1
+ fi
+
+ $CMD $TMP2 > $TMP3
+ if [ $? != 0 ]; then
+ echo "Error (step 3 of idempotency test): test.sh $1"
+ exit 1
+ fi
+
+ cmp -s $TMP2 $TMP3
+ if [ $? != 0 ]; then
+ diff $TMP2 $TMP3
+ echo "Error (step 4 of idempotency test): test.sh $1"
+ exit 1
+ fi
+}
+
+
+valid() {
+ cleanup
+ $CMD $1 > $TMP1
+ if [ $? != 0 ]; then
+ echo "Error (step 1 of validity test): test.sh $1"
+ exit 1
+ fi
+
+ $GC -o /dev/null $TMP1
+ if [ $? != 0 ]; then
+ echo "Error (step 2 of validity test): test.sh $1"
+ exit 1
+ fi
+}
+
+
+runtest() {
+ #echo "Testing silent mode"
+ cleanup
+ $1 silent $2
+
+ #echo "Testing idempotency"
+ cleanup
+ $1 idempotent $2
+}
+
+
+runtests() {
+ if [ $# == 0 ]; then
+ runtest apply
+ # verify the pretty-printed files can be compiled with $GC again
+ # do it in local directory only because of the prerequisites required
+ #echo "Testing validity"
+ cleanup
+ applydot valid
+ else
+ for F in $*; do
+ runtest apply1 $F
+ done
+ fi
+}
+
+
+# run over all .go files
+runtests $*
+cleanup
+
+# done
+echo
+echo "PASSED ($COUNT tests)"
diff --git a/src/make.bash b/src/make.bash
index 5a852a309a..6b103c6571 100755
--- a/src/make.bash
+++ b/src/make.bash
@@ -18,7 +18,7 @@ rm -f $HOME/bin/quietgcc
cp quietgcc.bash $HOME/bin/quietgcc
chmod +x $HOME/bin/quietgcc
-for i in lib9 libbio libmach_amd64 libregexp cmd pkg cmd/gobuild cmd/godoc
+for i in lib9 libbio libmach_amd64 libregexp cmd pkg cmd/gobuild cmd/godoc cmd/gofmt
do (
echo; echo; echo %%%% making $i %%%%; echo
cd $i
diff --git a/src/run.bash b/src/run.bash
index 1de6aafc27..f275d990ce 100755
--- a/src/run.bash
+++ b/src/run.bash
@@ -36,12 +36,10 @@ time make
GOMAXPROCS=10 make test
) || exit $?
-(xcd ../usr/gri/pretty
+(xcd cmd/gofmt
make clean
time make
-make smoketest
-# TODO: this belongs elsewhere
-cp godoc $HOME/bin
+time make smoketest
) || exit $?
(xcd ../doc/progs