forked from OSchip/llvm-project
Move the command line source location parsing from clang-cc.cpp into "include/Frontend/CommandLineSourceLoc.h".
llvm-svn: 74012
This commit is contained in:
parent
9cab2967c4
commit
25ee5db366
|
@ -0,0 +1,84 @@
|
||||||
|
//===--- CommandLineSourceLoc.h - Parsing for source locations-*- C++ -*---===//
|
||||||
|
//
|
||||||
|
// The LLVM Compiler Infrastructure
|
||||||
|
//
|
||||||
|
// This file is distributed under the University of Illinois Open Source
|
||||||
|
// License. See LICENSE.TXT for details.
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
//
|
||||||
|
// Command line parsing for source locations.
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
#ifndef LLVM_CLANG_FRONTEND_COMMANDLINESOURCELOC_H
|
||||||
|
#define LLVM_CLANG_FRONTEND_COMMANDLINESOURCELOC_H
|
||||||
|
|
||||||
|
#include "llvm/Support/CommandLine.h"
|
||||||
|
|
||||||
|
namespace clang {
|
||||||
|
|
||||||
|
/// \brief A source location that has been parsed on the command line.
|
||||||
|
struct ParsedSourceLocation {
|
||||||
|
std::string FileName;
|
||||||
|
unsigned Line;
|
||||||
|
unsigned Column;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace llvm {
|
||||||
|
namespace cl {
|
||||||
|
/// \brief Command-line option parser that parses source locations.
|
||||||
|
///
|
||||||
|
/// Source locations are of the form filename:line:column.
|
||||||
|
template<>
|
||||||
|
class parser<clang::ParsedSourceLocation>
|
||||||
|
: public basic_parser<clang::ParsedSourceLocation> {
|
||||||
|
public:
|
||||||
|
bool parse(Option &O, const char *ArgName,
|
||||||
|
const std::string &ArgValue,
|
||||||
|
clang::ParsedSourceLocation &Val);
|
||||||
|
};
|
||||||
|
|
||||||
|
bool
|
||||||
|
parser<clang::ParsedSourceLocation>::
|
||||||
|
parse(Option &O, const char *ArgName, const std::string &ArgValue,
|
||||||
|
clang::ParsedSourceLocation &Val) {
|
||||||
|
using namespace clang;
|
||||||
|
|
||||||
|
const char *ExpectedFormat
|
||||||
|
= "source location must be of the form filename:line:column";
|
||||||
|
std::string::size_type SecondColon = ArgValue.rfind(':');
|
||||||
|
if (SecondColon == std::string::npos) {
|
||||||
|
std::fprintf(stderr, "%s\n", ExpectedFormat);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
char *EndPtr;
|
||||||
|
long Column
|
||||||
|
= std::strtol(ArgValue.c_str() + SecondColon + 1, &EndPtr, 10);
|
||||||
|
if (EndPtr != ArgValue.c_str() + ArgValue.size()) {
|
||||||
|
std::fprintf(stderr, "%s\n", ExpectedFormat);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string::size_type FirstColon = ArgValue.rfind(':', SecondColon-1);
|
||||||
|
if (FirstColon == std::string::npos) {
|
||||||
|
std::fprintf(stderr, "%s\n", ExpectedFormat);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
long Line = std::strtol(ArgValue.c_str() + FirstColon + 1, &EndPtr, 10);
|
||||||
|
if (EndPtr != ArgValue.c_str() + SecondColon) {
|
||||||
|
std::fprintf(stderr, "%s\n", ExpectedFormat);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
Val.FileName = ArgValue.substr(0, FirstColon);
|
||||||
|
Val.Line = Line;
|
||||||
|
Val.Column = Column;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
|
@ -33,6 +33,7 @@
|
||||||
#include "clang/Frontend/PCHReader.h"
|
#include "clang/Frontend/PCHReader.h"
|
||||||
#include "clang/Frontend/TextDiagnosticBuffer.h"
|
#include "clang/Frontend/TextDiagnosticBuffer.h"
|
||||||
#include "clang/Frontend/TextDiagnosticPrinter.h"
|
#include "clang/Frontend/TextDiagnosticPrinter.h"
|
||||||
|
#include "clang/Frontend/CommandLineSourceLoc.h"
|
||||||
#include "clang/Frontend/Utils.h"
|
#include "clang/Frontend/Utils.h"
|
||||||
#include "clang/Analysis/PathDiagnostic.h"
|
#include "clang/Analysis/PathDiagnostic.h"
|
||||||
#include "clang/CodeGen/ModuleBuilder.h"
|
#include "clang/CodeGen/ModuleBuilder.h"
|
||||||
|
@ -79,85 +80,19 @@ using namespace clang;
|
||||||
// Source Location Parser
|
// Source Location Parser
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
/// \brief A source location that has been parsed on the command line.
|
static bool ResolveParsedLocation(ParsedSourceLocation &ParsedLoc,
|
||||||
struct ParsedSourceLocation {
|
FileManager &FileMgr,
|
||||||
std::string FileName;
|
RequestedSourceLocation &Result) {
|
||||||
unsigned Line;
|
const FileEntry *File = FileMgr.getFile(ParsedLoc.FileName);
|
||||||
unsigned Column;
|
|
||||||
|
|
||||||
/// \brief Try to resolve the file name of a parsed source location.
|
|
||||||
///
|
|
||||||
/// \returns true if there was an error, false otherwise.
|
|
||||||
bool ResolveLocation(FileManager &FileMgr, RequestedSourceLocation &Result);
|
|
||||||
};
|
|
||||||
|
|
||||||
bool
|
|
||||||
ParsedSourceLocation::ResolveLocation(FileManager &FileMgr,
|
|
||||||
RequestedSourceLocation &Result) {
|
|
||||||
const FileEntry *File = FileMgr.getFile(FileName);
|
|
||||||
if (!File)
|
if (!File)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
Result.File = File;
|
Result.File = File;
|
||||||
Result.Line = Line;
|
Result.Line = ParsedLoc.Line;
|
||||||
Result.Column = Column;
|
Result.Column = ParsedLoc.Column;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace llvm {
|
|
||||||
namespace cl {
|
|
||||||
/// \brief Command-line option parser that parses source locations.
|
|
||||||
///
|
|
||||||
/// Source locations are of the form filename:line:column.
|
|
||||||
template<>
|
|
||||||
class parser<ParsedSourceLocation>
|
|
||||||
: public basic_parser<ParsedSourceLocation> {
|
|
||||||
public:
|
|
||||||
bool parse(Option &O, const char *ArgName,
|
|
||||||
const std::string &ArgValue,
|
|
||||||
ParsedSourceLocation &Val);
|
|
||||||
};
|
|
||||||
|
|
||||||
bool
|
|
||||||
parser<ParsedSourceLocation>::
|
|
||||||
parse(Option &O, const char *ArgName, const std::string &ArgValue,
|
|
||||||
ParsedSourceLocation &Val) {
|
|
||||||
using namespace clang;
|
|
||||||
|
|
||||||
const char *ExpectedFormat
|
|
||||||
= "source location must be of the form filename:line:column";
|
|
||||||
std::string::size_type SecondColon = ArgValue.rfind(':');
|
|
||||||
if (SecondColon == std::string::npos) {
|
|
||||||
std::fprintf(stderr, "%s\n", ExpectedFormat);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
char *EndPtr;
|
|
||||||
long Column
|
|
||||||
= std::strtol(ArgValue.c_str() + SecondColon + 1, &EndPtr, 10);
|
|
||||||
if (EndPtr != ArgValue.c_str() + ArgValue.size()) {
|
|
||||||
std::fprintf(stderr, "%s\n", ExpectedFormat);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string::size_type FirstColon = ArgValue.rfind(':', SecondColon-1);
|
|
||||||
if (FirstColon == std::string::npos) {
|
|
||||||
std::fprintf(stderr, "%s\n", ExpectedFormat);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
long Line = std::strtol(ArgValue.c_str() + FirstColon + 1, &EndPtr, 10);
|
|
||||||
if (EndPtr != ArgValue.c_str() + SecondColon) {
|
|
||||||
std::fprintf(stderr, "%s\n", ExpectedFormat);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
Val.FileName = ArgValue.substr(0, FirstColon);
|
|
||||||
Val.Line = Line;
|
|
||||||
Val.Column = Column;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
// Global options.
|
// Global options.
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
@ -1979,8 +1914,8 @@ static void ProcessInputFile(Preprocessor &PP, PreprocessorFactory &PPF,
|
||||||
for (unsigned Idx = 0, Last = FixItAtLocations.size();
|
for (unsigned Idx = 0, Last = FixItAtLocations.size();
|
||||||
Idx != Last; ++Idx) {
|
Idx != Last; ++Idx) {
|
||||||
RequestedSourceLocation Requested;
|
RequestedSourceLocation Requested;
|
||||||
if (FixItAtLocations[Idx].ResolveLocation(PP.getFileManager(),
|
if (ResolveParsedLocation(FixItAtLocations[Idx],
|
||||||
Requested)) {
|
PP.getFileManager(), Requested)) {
|
||||||
fprintf(stderr, "FIX-IT could not find file \"%s\"\n",
|
fprintf(stderr, "FIX-IT could not find file \"%s\"\n",
|
||||||
FixItAtLocations[Idx].FileName.c_str());
|
FixItAtLocations[Idx].FileName.c_str());
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue