[cfe-commits] [PATCH] -ftabstop

Christian Adåker adaker at nada.kth.se
Sun Jan 10 12:23:30 PST 2010


On Sat, Jan 9, 2010 at 10:54 PM, Chris Lattner <clattner at apple.com> wrote:
> Please improve it in two ways: 1) write a testcase, 2) reject -ftabstop=0, which will cause a division by zero error.  Thanks!
>
> -Chris

Here is a test case, and a patch for checking that the TabStop value
is in a reasonable range (having an upper limit stops -ftabstop=-1
from consuming all available memory).

//Christian
-------------- next part --------------
diff --git test/Misc/tabstop.c test/Misc/tabstop.c
new file mode 100644
index 0000000..3fabda7
--- /dev/null
+++ test/Misc/tabstop.c
@@ -0,0 +1,30 @@
+// RUN: %clang_cc1 -ftabstop 3 -fsyntax-only %s 2>&1 | FileCheck -check-prefix=3 -strict-whitespace %s
+// RUN: %clang_cc1 -ftabstop 4 -fsyntax-only %s 2>&1 | FileCheck -check-prefix=4 -strict-whitespace %s
+// RUN: %clang_cc1 -ftabstop 5 -fsyntax-only %s 2>&1 | FileCheck -check-prefix=5 -strict-whitespace %s
+
+// tab
+	void* a = 1;
+
+// tab tab
+		void* b = 1;
+
+// 3x space tab
+   	void* c = 1;
+
+// tab at column 10
+void* d =	1;
+
+//CHECK-3: {{^   void\* a = 1;}}
+//CHECK-3: {{^      void\* b = 1;}}
+//CHECK-3: {{^      void\* c = 1;}}
+//CHECK-3: {{^void\* d =   1;}}
+
+//CHECK-4: {{^    void\* a = 1;}}
+//CHECK-4: {{^        void\* b = 1;}}
+//CHECK-4: {{^    void\* c = 1;}}
+//CHECK-4: {{^void\* d =   1;}}
+
+//CHECK-5: {{^     void\* a = 1;}}
+//CHECK-5: {{^          void\* b = 1;}}
+//CHECK-5: {{^     void\* c = 1;}}
+//CHECK-5: {{^void\* d = 1;}}
-------------- next part --------------
diff --git include/clang/Frontend/DiagnosticOptions.h include/clang/Frontend/DiagnosticOptions.h
index 0220e64..13039bb 100644
--- include/clang/Frontend/DiagnosticOptions.h
+++ include/clang/Frontend/DiagnosticOptions.h
@@ -37,6 +37,7 @@ public:
 
   /// The distance between tab stops.
   unsigned TabStop;
+  enum { DefaultTabStop = 8, MaxTabStop = 100 };
 
   /// Column limit for formatting message diagnostics, or 0 if unused.
   unsigned MessageLength;
@@ -52,7 +53,7 @@ public:
 public:
   DiagnosticOptions() {
     IgnoreWarnings = 0;
-    TabStop = 8;
+    TabStop = DefaultTabStop;
     MessageLength = 0;
     NoRewriteMacros = 0;
     Pedantic = 0;
diff --git lib/Frontend/CompilerInvocation.cpp lib/Frontend/CompilerInvocation.cpp
index 375c75c..7572599 100644
--- lib/Frontend/CompilerInvocation.cpp
+++ lib/Frontend/CompilerInvocation.cpp
@@ -222,7 +222,7 @@ static void DiagnosticOptsToArgs(const DiagnosticOptions &Opts,
     Res.push_back("-verify");
   if (Opts.ShowOptionNames)
     Res.push_back("-fdiagnostics-show-option");
-  if (Opts.TabStop != 8) {
+  if (Opts.TabStop != DiagnosticOptions::DefaultTabStop) {
     Res.push_back("-ftabstop");
     Res.push_back(llvm::utostr(Opts.TabStop));
   }
@@ -808,7 +808,10 @@ static void ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args,
   Opts.ShowOptionNames = Args.hasArg(OPT_fdiagnostics_show_option);
   Opts.ShowSourceRanges = Args.hasArg(OPT_fdiagnostics_print_source_range_info);
   Opts.VerifyDiagnostics = Args.hasArg(OPT_verify);
-  Opts.TabStop = getLastArgIntValue(Args, OPT_ftabstop, 8, Diags);
+  Opts.TabStop = getLastArgIntValue(Args, OPT_ftabstop,
+                                    DiagnosticOptions::DefaultTabStop, Diags);
+  if (Opts.TabStop == 0 || Opts.TabStop > DiagnosticOptions::MaxTabStop)
+    Opts.TabStop = DiagnosticOptions::DefaultTabStop;
   Opts.MessageLength = getLastArgIntValue(Args, OPT_fmessage_length, 0, Diags);
   Opts.DumpBuildInformation = getLastArgValue(Args, OPT_dump_build_information);
   Opts.Warnings = getAllArgValues(Args, OPT_W);
diff --git lib/Frontend/TextDiagnosticPrinter.cpp lib/Frontend/TextDiagnosticPrinter.cpp
index c27d112..fcefd4e 100644
--- lib/Frontend/TextDiagnosticPrinter.cpp
+++ lib/Frontend/TextDiagnosticPrinter.cpp
@@ -371,15 +371,17 @@ void TextDiagnosticPrinter::EmitCaretDiagnostic(SourceLocation Loc,
     CaretLine.push_back('^');
 
   // Scan the source line, looking for tabs.  If we find any, manually expand
-  // them to 8 characters and update the CaretLine to match.
+  // them to spaces and update the CaretLine to match.
   for (unsigned i = 0; i != SourceLine.size(); ++i) {
     if (SourceLine[i] != '\t') continue;
 
     // Replace this tab with at least one space.
     SourceLine[i] = ' ';
 
-    unsigned TabStop = DiagOpts->TabStop > 0 ? DiagOpts->TabStop : 8;
     // Compute the number of spaces we need to insert.
+    unsigned TabStop = DiagOpts->TabStop;
+    assert(0 < TabStop && TabStop <= DiagnosticOptions::MaxTabStop &&
+           "Invalid -ftabstop value");
     unsigned NumSpaces = ((i+TabStop)/TabStop * TabStop) - (i+1);
     assert(NumSpaces < TabStop && "Invalid computation of space amt");
 


More information about the cfe-commits mailing list