LCOV - code coverage report
Current view: top level - lib/Support - SpecialCaseList.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 76 77 98.7 %
Date: 2017-09-14 15:23:50 Functions: 10 10 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===-- SpecialCaseList.cpp - special case list for sanitizers ------------===//
       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             : // This is a utility class for instrumentation passes (like AddressSanitizer
      11             : // or ThreadSanitizer) to avoid instrumenting some functions or global
      12             : // variables, or to instrument some functions or global variables in a specific
      13             : // way, based on a user-supplied list.
      14             : //
      15             : //===----------------------------------------------------------------------===//
      16             : 
      17             : #include "llvm/Support/SpecialCaseList.h"
      18             : #include "llvm/ADT/SmallVector.h"
      19             : #include "llvm/ADT/StringExtras.h"
      20             : #include "llvm/ADT/StringSet.h"
      21             : #include "llvm/Support/MemoryBuffer.h"
      22             : #include "llvm/Support/Regex.h"
      23             : #include "llvm/Support/TrigramIndex.h"
      24             : #include <string>
      25             : #include <system_error>
      26             : #include <utility>
      27             : 
      28             : namespace llvm {
      29             : 
      30             : /// Represents a set of regular expressions.  Regular expressions which are
      31             : /// "literal" (i.e. no regex metacharacters) are stored in Strings, while all
      32             : /// others are represented as a single pipe-separated regex in RegEx.  The
      33             : /// reason for doing so is efficiency; StringSet is much faster at matching
      34             : /// literal strings than Regex.
      35        1511 : struct SpecialCaseList::Entry {
      36             :   StringSet<> Strings;
      37             :   TrigramIndex Trigrams;
      38             :   std::unique_ptr<Regex> RegEx;
      39             : 
      40         982 :   bool match(StringRef Query) const {
      41        1964 :     if (Strings.count(Query))
      42             :       return true;
      43         947 :     if (Trigrams.isDefinitelyOut(Query))
      44             :       return false;
      45         540 :     return RegEx && RegEx->match(Query);
      46             :   }
      47             : };
      48             : 
      49      231141 : SpecialCaseList::SpecialCaseList() : Entries(), Regexps(), IsCompiled(false) {}
      50             : 
      51             : std::unique_ptr<SpecialCaseList>
      52       77005 : SpecialCaseList::create(const std::vector<std::string> &Paths,
      53             :                         std::string &Error) {
      54      231015 :   std::unique_ptr<SpecialCaseList> SCL(new SpecialCaseList());
      55      308267 :   for (const auto &Path : Paths) {
      56             :     ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
      57         496 :         MemoryBuffer::getFile(Path);
      58         249 :     if (std::error_code EC = FileOrErr.getError()) {
      59          10 :       Error = (Twine("can't open file '") + Path + "': " + EC.message()).str();
      60           1 :       return nullptr;
      61             :     }
      62         495 :     std::string ParseError;
      63         744 :     if (!SCL->parse(FileOrErr.get().get(), ParseError)) {
      64           8 :       Error = (Twine("error parsing file '") + Path + "': " + ParseError).str();
      65           1 :       return nullptr;
      66             :     }
      67             :   }
      68       77003 :   SCL->compile();
      69             :   return SCL;
      70             : }
      71             : 
      72          42 : std::unique_ptr<SpecialCaseList> SpecialCaseList::create(const MemoryBuffer *MB,
      73             :                                                          std::string &Error) {
      74         126 :   std::unique_ptr<SpecialCaseList> SCL(new SpecialCaseList());
      75          42 :   if (!SCL->parse(MB, Error))
      76             :     return nullptr;
      77          39 :   SCL->compile();
      78             :   return SCL;
      79             : }
      80             : 
      81             : std::unique_ptr<SpecialCaseList>
      82       66445 : SpecialCaseList::createOrDie(const std::vector<std::string> &Paths) {
      83      132890 :   std::string Error;
      84      132890 :   if (auto SCL = create(Paths, Error))
      85      132890 :     return SCL;
      86           0 :   report_fatal_error(Error);
      87             : }
      88             : 
      89         290 : bool SpecialCaseList::parse(const MemoryBuffer *MB, std::string &Error) {
      90             :   // Iterate through each line in the blacklist file.
      91         580 :   SmallVector<StringRef, 16> Lines;
      92         580 :   SplitString(MB->getBuffer(), Lines, "\n\r");
      93         290 :   int LineNo = 1;
      94       13851 :   for (auto I = Lines.begin(), E = Lines.end(); I != E; ++I, ++LineNo) {
      95             :     // Ignore empty lines and lines starting with "#"
      96       26550 :     if (I->empty() || I->startswith("#"))
      97       14502 :       continue;
      98             :     // Get our prefix and unparsed regexp.
      99       11542 :     std::pair<StringRef, StringRef> SplitLine = I->split(":");
     100       11542 :     StringRef Prefix = SplitLine.first;
     101       11542 :     if (SplitLine.second.empty()) {
     102             :       // Missing ':' in the line.
     103          16 :       Error = (Twine("malformed line ") + Twine(LineNo) + ": '" +
     104          10 :                SplitLine.first + "'").str();
     105           6 :       return false;
     106             :     }
     107             : 
     108       11540 :     std::pair<StringRef, StringRef> SplitRegexp = SplitLine.second.split("=");
     109       12042 :     std::string Regexp = SplitRegexp.first;
     110       11540 :     StringRef Category = SplitRegexp.second;
     111             : 
     112             :     // See if we can store Regexp in Strings.
     113       34620 :     auto &Entry = Entries[Prefix][Category];
     114       22576 :     if (Regex::isLiteralERE(Regexp)) {
     115       33108 :       Entry.Strings.insert(Regexp);
     116             :       continue;
     117             :     }
     118        1512 :     Entry.Trigrams.insert(Regexp);
     119             : 
     120             :     // Replace * with .*
     121        1303 :     for (size_t pos = 0; (pos = Regexp.find('*', pos)) != std::string::npos;
     122         799 :          pos += strlen(".*")) {
     123         799 :       Regexp.replace(pos, strlen("*"), ".*");
     124             :     }
     125             : 
     126             :     // Check that the regexp is valid.
     127        1006 :     Regex CheckRE(Regexp);
     128        1006 :     std::string REError;
     129         504 :     if (!CheckRE.isValid(REError)) {
     130          16 :       Error = (Twine("malformed regex in line ") + Twine(LineNo) + ": '" +
     131          14 :                SplitLine.second + "': " + REError).str();
     132           4 :       return false;
     133             :     }
     134             : 
     135             :     // Add this regexp into the proper group by its prefix.
     136        1506 :     if (!Regexps[Prefix][Category].empty())
     137         678 :       Regexps[Prefix][Category] += "|";
     138        3012 :     Regexps[Prefix][Category] += "^" + Regexp + "$";
     139             :   }
     140             :   return true;
     141             : }
     142             : 
     143       77042 : void SpecialCaseList::compile() {
     144             :   assert(!IsCompiled && "compile() should only be called once");
     145             :   // Iterate through each of the prefixes, and create Regexs for them.
     146      231126 :   for (StringMap<StringMap<std::string>>::const_iterator I = Regexps.begin(),
     147      231126 :                                                          E = Regexps.end();
     148       77311 :        I != E; ++I) {
     149         538 :     for (StringMap<std::string>::const_iterator II = I->second.begin(),
     150         538 :                                                 IE = I->second.end();
     151         543 :          II != IE; ++II) {
     152        2740 :       Entries[I->getKey()][II->getKey()].RegEx.reset(new Regex(II->getValue()));
     153             :     }
     154             :   }
     155       77042 :   Regexps.clear();
     156       77042 :   IsCompiled = true;
     157       77042 : }
     158             : 
     159       75325 : SpecialCaseList::~SpecialCaseList() {}
     160             : 
     161      214892 : bool SpecialCaseList::inSection(StringRef Section, StringRef Query,
     162             :                                 StringRef Category) const {
     163             :   assert(IsCompiled && "SpecialCaseList::compile() was not called!");
     164      214892 :   StringMap<StringMap<Entry> >::const_iterator I = Entries.find(Section);
     165      429784 :   if (I == Entries.end()) return false;
     166        1185 :   StringMap<Entry>::const_iterator II = I->second.find(Category);
     167        2370 :   if (II == I->second.end()) return false;
     168             : 
     169        1964 :   return II->getValue().match(Query);
     170             : }
     171             : 
     172             : }  // namespace llvm

Generated by: LCOV version 1.13