summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clang-modernize/Core/IncludeExcludeInfo.cpp
blob: 2e4f1c21a5705800d23c35e372099d31f7f68452 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//===-- Core/IncludeExcludeInfo.cpp - IncludeExclude class impl -----------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// \brief This file provides the implementation of the IncludeExcludeInfo class
/// to handle the include and exclude command line options.
///
//===----------------------------------------------------------------------===//

#include "IncludeExcludeInfo.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"

using namespace llvm;
using std::error_code;

/// A string type to represent paths.
typedef SmallString<64> PathString;

namespace {
/// \brief Helper function to determine whether a file has the same path
/// prefix as \a Path.
///
/// \a Path must be an absolute path.
bool fileHasPathPrefix(StringRef File, StringRef Path) {
  // Converts File to its absolute path.
  PathString AbsoluteFile = File;
  sys::fs::make_absolute(AbsoluteFile);

  // Convert path strings to sys::path to iterate over each of its directories.
  sys::path::const_iterator FileI = sys::path::begin(AbsoluteFile),
                            FileE = sys::path::end(AbsoluteFile),
                            PathI = sys::path::begin(Path),
                            PathE = sys::path::end(Path);
  while (FileI != FileE && PathI != PathE) {
    // If the strings aren't equal then the two paths aren't contained within
    // each other.
    bool IsSeparator = ((FileI->size() == 1) && (PathI->size() == 1) &&
                        sys::path::is_separator((*FileI)[0]) &&
                        sys::path::is_separator((*PathI)[0]));
    if (!FileI->equals(*PathI) && !IsSeparator)
      return false;
    ++FileI;
    ++PathI;
  }
  return true;
}

/// \brief Helper function for removing relative operators from a given
/// path i.e. "..", ".".
/// \a Path must be a absolute path.
std::string removeRelativeOperators(StringRef Path) {
  sys::path::const_iterator PathI = sys::path::begin(Path);
  sys::path::const_iterator PathE = sys::path::end(Path);
  SmallVector<StringRef, 16> PathT;
  while (PathI != PathE) {
    if (PathI->equals("..")) {
      // Test if we have reached the root then Path is invalid.
      if (PathT.empty())
        return "";
      PathT.pop_back();
    } else if (!PathI->equals("."))
      PathT.push_back(*PathI);
    ++PathI;
  }
  // Rebuild the new path.
  PathString NewPath;
  for (SmallVectorImpl<StringRef>::iterator I = PathT.begin(), E = PathT.end();
       I != E; ++I) {
    llvm::sys::path::append(NewPath, *I);
  }
  return NewPath.str();
}

/// \brief Helper function to tokenize a string of paths and populate
/// the vector.
error_code parseCLInput(StringRef Line, std::vector<std::string> &List,
                        StringRef Separator) {
  SmallVector<StringRef, 32> Tokens;
  Line.split(Tokens, Separator, /*MaxSplit=*/ -1, /*KeepEmpty=*/ false);
  for (SmallVectorImpl<StringRef>::iterator I = Tokens.begin(),
                                            E = Tokens.end();
       I != E; ++I) {
    // Convert each path to its absolute path.
    PathString Path = I->rtrim();
    if (error_code Err = sys::fs::make_absolute(Path))
      return Err;
    // Remove relative operators from the path.
    std::string AbsPath = removeRelativeOperators(Path);
    // Add only non-empty paths to the list.
    if (!AbsPath.empty())
      List.push_back(AbsPath);
    else
      llvm::errs() << "Unable to parse input path: " << *I << "\n";

    llvm::errs() << "Parse: " <<List.back() << "\n";
  }
  return error_code();
}
} // end anonymous namespace

error_code IncludeExcludeInfo::readListFromString(StringRef IncludeString,
                                                  StringRef ExcludeString) {
  if (error_code Err = parseCLInput(IncludeString, IncludeList,
                                    /*Separator=*/ ","))
    return Err;
  if (error_code Err = parseCLInput(ExcludeString, ExcludeList,
                                    /*Separator=*/ ","))
    return Err;
  return error_code();
}

error_code IncludeExcludeInfo::readListFromFile(StringRef IncludeListFile,
                                                StringRef ExcludeListFile) {
  if (!IncludeListFile.empty()) {
    std::unique_ptr<MemoryBuffer> FileBuf;
    if (error_code Err = MemoryBuffer::getFile(IncludeListFile, FileBuf)) {
      errs() << "Unable to read from include file.\n";
      return Err;
    }
    if (error_code Err = parseCLInput(FileBuf->getBuffer(), IncludeList,
                                      /*Separator=*/ "\n"))
      return Err;
  }
  if (!ExcludeListFile.empty()) {
    std::unique_ptr<MemoryBuffer> FileBuf;
    if (error_code Err = MemoryBuffer::getFile(ExcludeListFile, FileBuf)) {
      errs() << "Unable to read from exclude file.\n";
      return Err;
    }
    if (error_code Err = parseCLInput(FileBuf->getBuffer(), ExcludeList,
                                      /*Separator=*/ "\n"))
      return Err;
  }
  return error_code();
}

bool IncludeExcludeInfo::isFileIncluded(StringRef FilePath) const {
  bool InIncludeList = false;

  for (std::vector<std::string>::const_iterator I = IncludeList.begin(),
                                                E = IncludeList.end();
       I != E; ++I)
    if ((InIncludeList = fileHasPathPrefix(FilePath, *I)))
      break;

  // If file is not in the list of included paths then it is not necessary
  // to check the excluded path list.
  if (!InIncludeList)
    return false;

  // If the file is in the included list but not is not explicitly excluded,
  // then it is safe to transform.
  return !isFileExplicitlyExcluded(FilePath);
}

bool IncludeExcludeInfo::isFileExplicitlyExcluded(StringRef FilePath) const {
  for (std::vector<std::string>::const_iterator I = ExcludeList.begin(),
                                                E = ExcludeList.end();
      I != E; ++I)
    if (fileHasPathPrefix(FilePath, *I))
      return true;

  return false;
}
OpenPOWER on IntegriCloud