From 6453f72b8d2ecd85eaef3bf3184fc6db0da5525d Mon Sep 17 00:00:00 2001 From: Daniel Dunbar Date: Wed, 25 Nov 2009 10:14:44 +0000 Subject: [PATCH] Add ParseSourceLocation::FromString, and simplify. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@89855 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Frontend/CommandLineSourceLoc.h | 49 +++++++++---------- 1 file changed, 22 insertions(+), 27 deletions(-) diff --git a/include/clang/Frontend/CommandLineSourceLoc.h b/include/clang/Frontend/CommandLineSourceLoc.h index d5a0598dfa..bea468b017 100644 --- a/include/clang/Frontend/CommandLineSourceLoc.h +++ b/include/clang/Frontend/CommandLineSourceLoc.h @@ -16,7 +16,7 @@ #define LLVM_CLANG_FRONTEND_COMMANDLINESOURCELOC_H #include "llvm/Support/CommandLine.h" -#include +#include "llvm/Support/raw_ostream.h" namespace clang { @@ -25,6 +25,23 @@ struct ParsedSourceLocation { std::string FileName; unsigned Line; unsigned Column; + +public: + /// Construct a parsed source location from a string; the Filename is empty on + /// error. + static ParsedSourceLocation FromString(llvm::StringRef Str) { + ParsedSourceLocation PSL; + std::pair ColSplit = Str.rsplit(':'); + std::pair LineSplit = + ColSplit.first.rsplit(':'); + + // If both tail splits were valid integers, return success. + if (!ColSplit.second.getAsInteger(10, PSL.Column) && + !LineSplit.second.getAsInteger(10, PSL.Line)) + PSL.FileName = LineSplit.first; + + return PSL; + } }; } @@ -48,35 +65,13 @@ namespace llvm { clang::ParsedSourceLocation &Val) { using namespace clang; - const char *ExpectedFormat - = "source location must be of the form filename:line:column"; - StringRef::size_type SecondColon = ArgValue.rfind(':'); - if (SecondColon == std::string::npos) { - std::fprintf(stderr, "%s\n", ExpectedFormat); - return true; - } - - unsigned Column; - if (ArgValue.substr(SecondColon + 1).getAsInteger(10, Column)) { - std::fprintf(stderr, "%s\n", ExpectedFormat); - return true; - } - ArgValue = ArgValue.substr(0, SecondColon); - - StringRef::size_type FirstColon = ArgValue.rfind(':'); - if (FirstColon == std::string::npos) { - std::fprintf(stderr, "%s\n", ExpectedFormat); - return true; - } - unsigned Line; - if (ArgValue.substr(FirstColon + 1).getAsInteger(10, Line)) { - std::fprintf(stderr, "%s\n", ExpectedFormat); + Val = ParsedSourceLocation::FromString(ArgValue); + if (Val.FileName.empty()) { + errs() << "error: " + << "source location must be of the form filename:line:column\n"; return true; } - Val.FileName = ArgValue.substr(0, FirstColon); - Val.Line = Line; - Val.Column = Column; return false; } } -- 2.50.1