blob: fcf66ee8a6190c45e9f126621466a363b504c7db [file] [log] [blame]
Alexander Kornienkodad4acb2014-05-22 16:07:11 +00001//===--- ClangTidyOptions.cpp - clang-tidy ----------------------*- 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#include "ClangTidyOptions.h"
11#include "llvm/Support/YAMLTraits.h"
12
13using clang::tidy::FileFilter;
14
15LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(FileFilter)
16LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(FileFilter::LineRange)
17
18namespace llvm {
19namespace yaml {
20
21// Map std::pair<int, int> to a JSON array of size 2.
22template <> struct SequenceTraits<FileFilter::LineRange> {
23 static size_t size(IO &IO, FileFilter::LineRange &Range) {
24 return Range.first == 0 ? 0 : Range.second == 0 ? 1 : 2;
25 }
26 static unsigned &element(IO &IO, FileFilter::LineRange &Range, size_t Index) {
27 if (Index > 1)
28 IO.setError("Too many elements in line range.");
29 return Index == 0 ? Range.first : Range.second;
30 }
31};
32
33template <> struct MappingTraits<FileFilter> {
34 static void mapping(IO &IO, FileFilter &File) {
35 IO.mapRequired("name", File.Name);
36 IO.mapOptional("lines", File.LineRanges);
37 }
38 static StringRef validate(IO &io, FileFilter &File) {
39 if (File.Name.empty())
40 return "No file name specified";
41 for (const FileFilter::LineRange &Range : File.LineRanges) {
42 if (Range.first <= 0 || Range.second <= 0)
43 return "Invalid line range";
44 }
45 return StringRef();
46 }
47};
48
49} // namespace yaml
50} // namespace llvm
51
52namespace clang {
53namespace tidy {
54
55/// \brief Parses -line-filter option and stores it to the \c Options.
56llvm::error_code parseLineFilter(const std::string &LineFilter,
57 clang::tidy::ClangTidyOptions &Options) {
58 llvm::yaml::Input Input(LineFilter);
59 Input >> Options.LineFilter;
60 return Input.error();
61}
62
63} // namespace tidy
64} // namespace clang