Move the command line source location parsing from clang-cc.cpp into "include/Fronten...
[clang.git] / include / clang / Frontend / CommandLineSourceLoc.h
blob7ee2a5816c8dff4eec5f36a44bbb881958a3b2f3
1 //===--- CommandLineSourceLoc.h - Parsing for source locations-*- C++ -*---===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Command line parsing for source locations.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_FRONTEND_COMMANDLINESOURCELOC_H
15 #define LLVM_CLANG_FRONTEND_COMMANDLINESOURCELOC_H
17 #include "llvm/Support/CommandLine.h"
19 namespace clang {
21 /// \brief A source location that has been parsed on the command line.
22 struct ParsedSourceLocation {
23 std::string FileName;
24 unsigned Line;
25 unsigned Column;
30 namespace llvm {
31 namespace cl {
32 /// \brief Command-line option parser that parses source locations.
33 ///
34 /// Source locations are of the form filename:line:column.
35 template<>
36 class parser<clang::ParsedSourceLocation>
37 : public basic_parser<clang::ParsedSourceLocation> {
38 public:
39 bool parse(Option &O, const char *ArgName,
40 const std::string &ArgValue,
41 clang::ParsedSourceLocation &Val);
44 bool
45 parser<clang::ParsedSourceLocation>::
46 parse(Option &O, const char *ArgName, const std::string &ArgValue,
47 clang::ParsedSourceLocation &Val) {
48 using namespace clang;
50 const char *ExpectedFormat
51 = "source location must be of the form filename:line:column";
52 std::string::size_type SecondColon = ArgValue.rfind(':');
53 if (SecondColon == std::string::npos) {
54 std::fprintf(stderr, "%s\n", ExpectedFormat);
55 return true;
57 char *EndPtr;
58 long Column
59 = std::strtol(ArgValue.c_str() + SecondColon + 1, &EndPtr, 10);
60 if (EndPtr != ArgValue.c_str() + ArgValue.size()) {
61 std::fprintf(stderr, "%s\n", ExpectedFormat);
62 return true;
65 std::string::size_type FirstColon = ArgValue.rfind(':', SecondColon-1);
66 if (FirstColon == std::string::npos) {
67 std::fprintf(stderr, "%s\n", ExpectedFormat);
68 return true;
70 long Line = std::strtol(ArgValue.c_str() + FirstColon + 1, &EndPtr, 10);
71 if (EndPtr != ArgValue.c_str() + SecondColon) {
72 std::fprintf(stderr, "%s\n", ExpectedFormat);
73 return true;
76 Val.FileName = ArgValue.substr(0, FirstColon);
77 Val.Line = Line;
78 Val.Column = Column;
79 return false;
84 #endif