From bb1b44f0047e1ed3b85e52361c8bf29dcd404706 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Mon, 16 Jul 2007 06:55:01 +0000 Subject: [PATCH] Make octal constant lexing use AdvanceToTokenCharacter to give more accurate diagnostics. For test/Lexer/comments.c we now emit: int x = 000000080; /* expected-error {{invalid digit}} */ ^ constants.c:7:4: error: invalid digit '8' in octal constant 00080; /* expected-error {{invalid digit}} */ ^ The last line is due to an escaped newline. The full line looks like: int y = 0000\ 00080; /* expected-error {{invalid digit}} */ Previously, we emitted: constants.c:4:9: error: invalid digit '8' in octal constant int x = 000000080; /* expected-error {{invalid digit}} */ ^ constants.c:6:9: error: invalid digit '8' in octal constant int y = 0000\ ^ which isn't too bad, but the new way is better for the user, regardless of whether there is an escaped newline or not. All the other lexer-related diagnostics should switch over to using AdvanceToTokenCharacter where appropriate. Help wanted :). This implements test/Lexer/constants.c. llvm-svn: 39906 --- clang/Basic/SourceManager.cpp | 1 - clang/Lex/LiteralSupport.cpp | 4 +++- clang/TODO.txt | 2 ++ clang/clang.xcodeproj/project.pbxproj | 2 +- clang/test/Lexer/constants.c | 8 ++++++++ 5 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 clang/test/Lexer/constants.c diff --git a/clang/Basic/SourceManager.cpp b/clang/Basic/SourceManager.cpp index a2520e3aff3e..2177c982d453 100644 --- a/clang/Basic/SourceManager.cpp +++ b/clang/Basic/SourceManager.cpp @@ -339,7 +339,6 @@ unsigned SourceManager::getSourceFilePos(SourceLocation Loc) const { return getFilePos(Loc); } - /// PrintStats - Print statistics to stderr. /// void SourceManager::PrintStats() const { diff --git a/clang/Lex/LiteralSupport.cpp b/clang/Lex/LiteralSupport.cpp index 5d9c7bd11eb5..8978c796a8cc 100644 --- a/clang/Lex/LiteralSupport.cpp +++ b/clang/Lex/LiteralSupport.cpp @@ -14,8 +14,9 @@ #include "clang/Lex/LiteralSupport.h" #include "clang/Lex/Preprocessor.h" -#include "clang/Basic/TargetInfo.h" #include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Basic/TargetInfo.h" #include "llvm/ADT/APInt.h" #include "llvm/ADT/StringExtras.h" using namespace clang; @@ -261,6 +262,7 @@ NumericLiteralParser(const char *begin, const char *end, if (s == ThisTokEnd) { // Done. } else if (isxdigit(*s)) { + TokLoc = PP.AdvanceToTokenCharacter(TokLoc, s-begin); Diag(TokLoc, diag::err_invalid_octal_digit, std::string(s, s+1)); return; } else if (*s == '.') { diff --git a/clang/TODO.txt b/clang/TODO.txt index d2b944bb6bea..94e471dcdd4c 100644 --- a/clang/TODO.txt +++ b/clang/TODO.txt @@ -22,6 +22,8 @@ diag.c:4:9: error: invalid digit '8' in octal constant 00080; ^ +This specific diagnostic is implemented, but others should be updated. + //===---------------------------------------------------------------------===// diff --git a/clang/clang.xcodeproj/project.pbxproj b/clang/clang.xcodeproj/project.pbxproj index 560076196ae1..6059d2ac79ee 100644 --- a/clang/clang.xcodeproj/project.pbxproj +++ b/clang/clang.xcodeproj/project.pbxproj @@ -189,7 +189,7 @@ 1A869AA70BA21ABA008DA07A /* LiteralSupport.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = LiteralSupport.cpp; sourceTree = ""; }; 84D9A8870C1A57E100AC7ABC /* AttributeList.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = AttributeList.cpp; path = Parse/AttributeList.cpp; sourceTree = ""; }; 84D9A88B0C1A581300AC7ABC /* AttributeList.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = AttributeList.h; path = clang/Parse/AttributeList.h; sourceTree = ""; }; - 8DD76F6C0486A84900D96B5E /* clang */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = clang; sourceTree = BUILT_PRODUCTS_DIR; }; + 8DD76F6C0486A84900D96B5E /* clang */ = {isa = PBXFileReference; includeInIndex = 0; lastKnownFileType = "compiled.mach-o.executable"; path = clang; sourceTree = BUILT_PRODUCTS_DIR; }; DE01DA480B12ADA300AC22CE /* PPCallbacks.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = PPCallbacks.h; sourceTree = ""; }; DE06756B0C051CFE00EBBFD8 /* ParseExprCXX.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = ParseExprCXX.cpp; path = Parse/ParseExprCXX.cpp; sourceTree = ""; }; DE06B73D0A8307640050E87E /* LangOptions.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = LangOptions.h; sourceTree = ""; }; diff --git a/clang/test/Lexer/constants.c b/clang/test/Lexer/constants.c new file mode 100644 index 000000000000..f7e4cd02fbb0 --- /dev/null +++ b/clang/test/Lexer/constants.c @@ -0,0 +1,8 @@ +/* RUN: clang -parse-ast-check %s + */ + +int x = 000000080; /* expected-error {{invalid digit}} */ + +int y = 0000\ +00080; /* expected-error {{invalid digit}} */ +