text:
/*
Package regexp2 is a regexp package that has an interface similar to Go's framework regexp engine but uses a
more feature full regex engine behind the scenes.

It doesn't have constant time guarantees, but it allows backtracking and is compatible with Perl5 and .NET.
You'll likely be better off with the RE2 engine from the regexp package and should only use this if you
need to write very complex patterns or require compatibility with .NET.
*/
package regexp2

import (
	"errors"
	"math"
	"strconv"
	"sync"
	"time"

	"github.com/dlclark/regexp2/syntax"
)

// Default timeout used when running regexp matches -- "forever"
var DefaultMatchTimeout = time.Duration(math.MaxInt64)

// Regexp is the representation of a compiled regular expression.
// A Regexp is safe for concurrent use by multiple goroutines.
type Regexp struct {
	//timeout when trying to find matches
	MatchTimeout time.Duration

	// read-only after Compile
	pattern string       // as passed to Compile
	options RegexOptions // options

	caps     map[int]int    // capnum->index
	capnames map[string]int //capture group name -> index
	capslist []string       //sorted list of capture group names
	capsize  int            // size of the capture array

	code *syntax.Code // compiled program

	// cache of machines for running regexp
	muRun  sync.Mutex
	runner []*runner
}

// Compile parses a regular expression and returns, if successful,
// a Regexp object that can be used to match against text.
func Compile(expr string, opt RegexOptions) (*Regexp, error) {
	// parse it
	tree, err := syntax.Parse(expr, syntax.RegexOptions(opt))
	if err != nil {
		return nil, err
	}

	// translate it to code
	code, err := syntax.Write(tree)
	if err != nil {
		return nil, err
	}

	// return it
	return &Regexp{
		pattern:      expr,
		options:      opt,
		caps:         code.Caps,
		capnames:     tree.Capnames,
		capslist:     tree.Caplist,
		capsize:      code.Capsize,
		code:         code,
		MatchTimeout: DefaultMatchTimeout,
	}, nil
}

// MustCompile is like Compile but panics if the expression cannot be parsed.
// It simplifies safe initialization of global variables holding compiled regular
// expressions.
func MustCompile(str string, opt RegexOptions) *Regexp {
	regexp, error := Compile(str, opt)
	if error != nil {
		panic(`regexp2: Compile(` + quote(str) + `): ` + error.Error())
	}
	return regexp
}

// Escape adds backslashes to any special characters in the input string
func Escape(input string) string {
	return syntax.Escape(input)
}

// Unescape removes any backslashes from previously-escaped special characters in the input string
func Unescape(input string) (string, error) {
	return syntax.Unescape(input)
}

// String returns the source text used to compile the regular expression.
func (re *Regexp) String() string {
	return re.pattern
}

func quote(s string) string {
	if strconv.CanBackquote(s) {
		return "`" + s + "`"
	}
	return strconv.Quote(s)
}

// RegexOptions impact the runtime and parsing behavior
// for each specific regex.  They are setable in code as well
// as in the regex pattern itself.
type RegexOptions int32

const (
	None                    RegexOptions = 0x0
	IgnoreCase                           = 0x0001 // "i"
	Multiline                            = 0x0002 // "m"
	ExplicitCapture                      = 0x0004 // "n"
	Compiled                             = 0x0008 // "c"
	Singleline                           = 0x0010 // "s"
	IgnorePatternWhitespace              = 0x0020 // "x"
	RightToLeft                          = 0x0040 // "r"
	Debug                                = 0x0080 // "d"
	ECMAScript                           = 0x0100 // "e"
	RE2                                  = 0x0200 // RE2 (regexp package) compatibility mode
)

func (re *Regexp) RightToLeft() bool {
	return re.options&RightToLeft != 0
}

func (re *Regexp) Debug() bool {
	return re.options&Debug != 0
}

// Replace searches the input string and replaces each match found with the replacement text.
// Count will limit the number of matches attempted and startAt will allow
// us to skip past possible matches at the start of the input (left or right depending on RightToLeft option).
// Set startAt and count to -1 to go through the whole string
func (re *Regexp) Replace(input, replacement string, startAt, count int) (string, error) {
	data, err := syntax.NewReplacerData(replacement, re.caps, re.capsize, re.capnames, syntax.RegexOptions(re.options))
	if err != nil {
		return "", err
	}
	//TODO: cache ReplacerData

	return replace(re, data, nil, input, startAt, count)
}

// ReplaceFunc searches the input string and replaces each match found using the string from the evaluator
// Count will limit the number of matches attempted and startAt will allow
// us to skip past possible matches at the start of the input (left or right depending on RightToLeft option).
// Set startAt and count to -1 to go through the whole string.
func (re *Regexp) ReplaceFunc(input string, evaluator MatchEvaluator, startAt, count int) (string, error) {
	return replace(re, nil, evaluator, input, startAt, count)
}

// FindStringMatch searches the input string for a Regexp match
func (re *Regexp) FindStringMatch(s string) (*Match, error) {
	// convert string to runes
	return re.run(false, -1, getRunes(s))
}

// FindRunesMatch searches the input rune slice for a Regexp match
func (re *Regexp) FindRunesMatch(r []rune) (*Match, error) {
	return re.run(false, -1, r)
}

// FindStringMatchStartingAt searches the input string for a Regexp match starting at the startAt index
func (re *Regexp) FindStringMatchStartingAt(s string, startAt int) (*Match, error) {
	if startAt > len(s) {
		return nil, errors.New("startAt must be less than the length of the input string")
	}
	r, startAt := re.getRunesAndStart(s, startAt)
	if startAt == -1 {
		// we didn't find our start index in the string -- that's a problem
		return nil, errors.New("startAt must align to the start of a valid rune in the input string")
	}

	return re.run(false, startAt, r)
}

// FindRunesMatchStartingAt searches the input rune slice for a Regexp match starting at the startAt index
func (re *Regexp) FindRunesMatchStartingAt(r []rune, startAt int) (*Match, error) {
	return re.run(false, startAt, r)
}

// FindNextMatch returns the next match in the same input string as the match parameter.
// Will return nil if there is no next match or if given a nil match.
func (re *Regexp) FindNextMatch(m *Match) (*Match, error) {
	if m == nil {
		return nil, nil
	}

	// If previous match was empty, advance by one before matching to prevent
	// infinite loop
	startAt := m.textpos
	if m.Length == 0 {
		if m.textpos == len(m.text) {
			return nil, nil
		}

		if re.RightToLeft() {
			startAt--
		} else {
			startAt++
		}
	}
	return re.run(false, startAt, m.text)
}

// MatchString return true if the string matches the regex
// error will be set if a timeout occurs
func (re *Regexp) MatchString(s string) (bool, error) {
	m, err := re.run(true, -1, getRunes(s))
	if err != nil {
		return false, err
	}
	return m != nil, nil
}

func (re *Regexp) getRunesAndStart(s string, startAt int) ([]rune, int) {
	if startAt < 0 {
		if re.RightToLeft() {
			r := getRunes(s)
			return r, len(r)
		}
		return getRunes(s), 0
	}
	ret := make([]rune, len(s))
	i := 0
	runeIdx := -1
	for strIdx, r := range s {
		if strIdx == startAt {
			runeIdx = i
		}
		ret[i] = r
		i++
	}
	return ret[:i], runeIdx
}

func getRunes(s string) []rune {
	ret := make([]rune, len(s))
	i := 0
	for _, r := range s {
		ret[i] = r
		i++
	}
	return ret[:i]
}

// MatchRunes return true if the runes matches the regex
// error will be set if a timeout occurs
func (re *Regexp) MatchRunes(r []rune) (bool, error) {
	m, err := re.run(true, -1, r)
	if err != nil {
		return false, err
	}
	return m != nil, nil
}

// GetGroupNames Returns the set of strings used to name capturing groups in the expression.
func (re *Regexp) GetGroupNames() []string {
	var result []string

	if re.capslist == nil {
		result = make([]string, re.capsize)

		for i := 0; i < len(result); i++ {
			result[i] = strconv.Itoa(i)
		}
	} else {
		result = make([]string, len(re.capslist))
		copy(result, re.capslist)
	}

	return result
}

// GetGroupNumbers returns the integer group numbers corresponding to a group name.
func (re *Regexp) GetGroupNumbers() []int {
	var result []int

	if re.caps == nil {
		result = make([]int, re.capsize)

		for i := 0; i < len(result); i++ {
			result[i] = i
		}
	} else {
		result = make([]int, len(re.caps))

		for k, v := range re.caps {
			result[v] = k
		}
	}

	return result
}

// GroupNameFromNumber retrieves a group name that corresponds to a group number.
// It will return "" for and unknown group number.  Unnamed groups automatically
// receive a name that is the decimal string equivalent of its number.
func (re *Regexp) GroupNameFromNumber(i int) string {
	if re.capslist == nil {
		if i >= 0 && i < re.capsize {
			return strconv.Itoa(i)
		}

		return ""
	}

	if re.caps != nil {
		var ok bool
		if i, ok = re.caps[i]; !ok {
			return ""
		}
	}

	if i >= 0 && i < len(re.capslist) {
		return re.capslist[i]
	}

	return ""
}

// GroupNumberFromName returns a group number that corresponds to a group name.
// Returns -1 if the name is not a recognized group name.  Numbered groups
// automatically get a group name that is the decimal string equivalent of its number.
func (re *Regexp) GroupNumberFromName(name string) int {
	// look up name if we have a hashtable of names
	if re.capnames != nil {
		if k, ok := re.capnames[name]; ok {
			return k
		}

		return -1
	}

	// convert to an int if it looks like a number
	result := 0
	for i := 0; i < len(name); i++ {
		ch := name[i]

		if ch > '9' || ch < '0' {
			return -1
		}

		result *= 10
		result += int(ch - '0')
	}

	// return int if it's in range
	if result >= 0 && result < re.capsize {
		return result
	}

	return -1
}

redpajama_set_name: RedPajamaGithub


text:
import { Component } from '@angular/core';
import { DomSanitizationService } from '@angular/platform-browser';
import { KurentoRoom } from '../KurentoRoom'
import { Room } from '../Room'
import { Stream } from '../Stream'
import { ServiceRoom } from '../room.service'
import { ServiceParticipant } from '../participant.service'
import { KurentoroomService } from '../kurentoroom.service'
import { ROUTER_DIRECTIVES, Router } from "@angular/router";

declare var jQuery: any;
declare var $:any;
@Component({
	moduleId: module.id,
	selector: 'call',
	templateUrl: 'call.component.html',
	styleUrls: ['call.component.css']
})

export class CallComponent {

    private roomName:string = this.kurentoroomService.getRoomName();
    private userName:string = this.kurentoroomService.getUserName();
    private participants:any[] = this.serviceParticipant.getParticipants();
    private kurento:KurentoRoom = this.serviceRoom.getKurento();
    private message:any;

    constructor(private router: Router, private sanitizer: DomSanitizationService, private kurentoroomService: KurentoroomService, private serviceRoom: ServiceRoom, private serviceParticipant: ServiceParticipant) {
        if (this.kurentoroomService.getRoomName()==undefined){
            this.router.navigate(['/']);
        }else{
            this.kurentoroomService.connect();
        }
    }

    leaveRoom () {
        this.serviceRoom.getKurento().close();
        this.serviceParticipant.removeParticipants();
        this.router.navigate(["/index"]);
    };

    /*window.onbeforeunload = function () {
    	//not necessary if not connected
    	if (this.serviceParticipant.isConnected()) {
    		this.serviceRoom.getKurento().close();
    	}
    };*/

    goFullscreen  () {

        /*if (Fullscreen.isEnabled())
            Fullscreen.cancel();
        else
            Fullscreen.all();*/
    };

    disableMainSpeaker (value) {
    	let element = document.getElementById("buttonMainSpeaker");
        if (element.classList.contains("md-person")) { //on
            element.classList.remove("md-person");
            element.classList.add("md-recent-actors");
            this.serviceParticipant.enableMainSpeaker();
        } else { //off
            element.classList.remove("md-recent-actors");
            element.classList.add("md-person");
            this.serviceParticipant.disableMainSpeaker();
        }
    }

    onOffVolume () {
        let localStream =  this.serviceRoom.getLocalStream();
        let element = document.getElementById("buttonVolume");
        if (element.classList.contains("md-volume-off")) { //on
            element.classList.remove("md-volume-off");
            element.classList.add("md-volume-up");
            localStream.audioEnabled = true;
        } else { //off
            element.classList.remove("md-volume-up");
            element.classList.add("md-volume-off");
            localStream.audioEnabled = false;
        }
    };

    onOffVideocam  () {
        let localStream =  this.serviceRoom.getLocalStream();
        let element = document.getElementById("buttonVideocam");
        if (element.classList.contains("md-videocam-off")) {//on
            element.classList.remove("md-videocam-off");
            element.classList.add("md-videocam");
            localStream.videoEnabled = true;
        } else {//off
            element.classList.remove("md-videocam");
            element.classList.add("md-videocam-off");
            localStream.videoEnabled = false;
        }
    };

    disconnectStream () {
    	let localStream =  this.serviceRoom.getLocalStream();
    	let participant = this.serviceParticipant.getMainParticipant();
    	if (!localStream || !participant) {
    		/*LxNotificationService.alert('Error!', "Not connected yet", 'Ok', function(answer) {
            });*/
            return false;
        }
        this.serviceParticipant.disconnectParticipant(participant);
        this.serviceRoom.getKurento().disconnectParticipant(participant.getStream());
    }

    sendMessage () {
        console.log("Sending message", this.message);
        let kurento = this.serviceRoom.getKurento();
        kurento.sendMessage(this.roomName, this.userName, this.message);
        this.message = "";
    };

    //open or close chat when click in chat button
    toggleChat () {
        let selectedEffect = "slide";
        // most effect types need no options passed by default
        let options = {direction: "right"};
        if ($("#effect").is(':visible')) {
            $("#content").animate({width: '100%'}, 500);
        } else {
            $("#content").animate({width: '80%'}, 500);
        }
        // run the effect
        $("#effect").toggle(selectedEffect, options, 500);
    };

    showHat () {
    	let targetHat = false;
    	let offImgStyle = "md-mood";
    	let offColorStyle = "btn--deep-purple";
    	let onImgStyle = "md-face-unlock";
    	let onColorStyle = "btn--purple";
    	let element = document.getElementById("hatButton");
        if (element.classList.contains(offImgStyle)) { //off
            element.classList.remove(offImgStyle);
            element.classList.remove(offColorStyle);
            element.classList.add(onImgStyle);
            element.classList.add(onColorStyle);
            targetHat = true;
        } else if (element.classList.contains(onImgStyle)) { //on
            element.classList.remove(onImgStyle);
            element.classList.remove(onColorStyle);
            element.classList.add(offImgStyle);
            element.classList.add(offColorStyle);
            targetHat = false;
        }

        let hatTo = targetHat ? "on" : "off";
        console.log("Toggle hat to " + hatTo);
        this.serviceRoom.getKurento().sendCustomRequest({hat: targetHat}, (error:any, response:any) => {
            if (error) {
                console.error("Unable to toggle hat " + hatTo, error);
                /*LxNotificationService.alert('Error!', "Unable to toggle hat " + hatTo,
                'Ok', function(answer) {});*/
                return false;
            } else {
            	console.debug("Response on hat toggle", response);
            }
        });
    };
}


text:
"""Testing for transformers."""

# Author: Johann Faouzi <johann.faouzi@gmail.com>
# License: BSD-3-Clause

import numpy as np
from scipy.stats import boxcox, norm, yeojohnson
from pyts.preprocessing import StandardScaler
from pyts.preprocessing import PowerTransformer, QuantileTransformer

X = np.arange(1, 34).reshape(3, 11)

def test_actual_results_power_transformer_box_cox():
    """Test that the actual results are the expected ones."""
    for standardize in [True, False]:
        pt = PowerTransformer(method='box-cox', standardize=standardize)
        arr_actual = pt.fit_transform(X)
        arr_desired = [boxcox(X[i])[0] for i in range(3)]
        if standardize:
            arr_desired = StandardScaler().transform(arr_desired)
        np.testing.assert_allclose(arr_actual, arr_desired, atol=1e-5, rtol=0.)

def test_actual_results_power_transformer_yeo_johnson():
    """Test that the actual results are the expected ones."""
    for standardize in [True, False]:
        pt = PowerTransformer(method='yeo-johnson', standardize=standardize)
        arr_actual = pt.fit_transform(X)
        arr_desired = [yeojohnson(X[i].astype('float64'))[0] for i in range(3)]
        if standardize:
            arr_desired = StandardScaler().transform(arr_desired)
        np.testing.assert_allclose(arr_actual, arr_desired, atol=1e-5, rtol=0.)

def test_actual_results_quantile_transformer_uniform():
    """Test that the actual results are the expected ones."""
    transformer = QuantileTransformer(n_quantiles=11)
    arr_actual = transformer.fit_transform(X)
    arr_desired = [np.linspace(0, 1, 11) for _ in range(3)]
    np.testing.assert_allclose(arr_actual, arr_desired, atol=1e-5, rtol=0.)

def test_actual_results_quantile_transformer_normal():
    """Test that the actual results are the expected ones."""
    X_ppf = norm.ppf(np.linspace(0, 1, 1000)[1:-1])
    weights = np.round(norm.pdf(X_ppf) * 1000).astype('int64')
    X = []
    for value, weight in zip(X_ppf, weights):
        X += [value] * weight
    X = np.asarray(X).reshape(1, -1)
    transformer = QuantileTransformer(n_quantiles=11,
                                      output_distribution='normal')
    arr_actual = transformer.fit_transform(X)
    arr_desired = X
    atol = 0.01 * X.shape[1]
    np.testing.assert_allclose(arr_actual, arr_desired, atol=atol, rtol=0.)


text:
=head1 LICENSE
 Copyright [2009-2019] EMBL-European Bioinformatics Institute

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

=head1 CONTACT

  Please email comments or questions to the public Ensembl
  developers list at <dev@ensembl.org>.

  Questions may also be sent to the Ensembl help desk at
  <helpdesk@ensembl.org>.

=head1 DESCRIPTION

This script is an example of how to use Bio::EnsEMBL::MetaData::DBSQL::GenomeInfoAdaptor
to find and print information about genomes with variation data in the latest release of Ensembl Genomes

=head1 AUTHOR

dstaines

=head1 MAINTANER

$Author$

=head1 VERSION

$Revision$
=cut

use strict;
use warnings;

use Bio::EnsEMBL::MetaData::DBSQL::GenomeInfoAdaptor;

# create an adaptor to work with genomes
my $gdba = Bio::EnsEMBL::MetaData::DBSQL::GenomeInfoAdaptor->build_ensembl_genomes_adaptor();

# find and iterate over all genomes
for my $genome (@{$gdba->fetch_all_with_variation()}) {
	print $genome->name()."\n";
}


text:
//===--- PlistDiagnostics.cpp - Plist Diagnostics for Paths -----*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
//  This file defines the PlistDiagnostics object.
//
//===----------------------------------------------------------------------===//

#include "clang/Analysis/PathDiagnostic.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/PlistSupport.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Basic/Version.h"
#include "clang/CrossTU/CrossTranslationUnit.h"
#include "clang/Frontend/ASTUnit.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Lex/TokenConcatenation.h"
#include "clang/Rewrite/Core/HTMLRewrite.h"
#include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
#include "clang/StaticAnalyzer/Core/IssueHash.h"
#include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Support/Casting.h"

using namespace clang;
using namespace ento;
using namespace markup;

//===----------------------------------------------------------------------===//
// Declarations of helper classes and functions for emitting bug reports in
// plist format.
//===----------------------------------------------------------------------===//

namespace {
  class PlistDiagnostics : public PathDiagnosticConsumer {
    const std::string OutputFile;
    const Preprocessor &PP;
    const cross_tu::CrossTranslationUnitContext &CTU;
    AnalyzerOptions &AnOpts;
    const bool SupportsCrossFileDiagnostics;
  public:
    PlistDiagnostics(AnalyzerOptions &AnalyzerOpts, const std::string &prefix,
                     const Preprocessor &PP,
                     const cross_tu::CrossTranslationUnitContext &CTU,
                     bool supportsMultipleFiles);

    ~PlistDiagnostics() override {}

    void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
                              FilesMade *filesMade) override;

    StringRef getName() const override {
      return "PlistDiagnostics";
    }

    PathGenerationScheme getGenerationScheme() const override {
      return Extensive;
    }
    bool supportsLogicalOpControlFlow() const override { return true; }
    bool supportsCrossFileDiagnostics() const override {
      return SupportsCrossFileDiagnostics;
    }
  };
} // end anonymous namespace

namespace {

/// A helper class for emitting a single report.
class PlistPrinter {
  const FIDMap& FM;
  AnalyzerOptions &AnOpts;
  const Preprocessor &PP;
  const cross_tu::CrossTranslationUnitContext &CTU;
  llvm::SmallVector<const PathDiagnosticMacroPiece *, 0> MacroPieces;

public:
  PlistPrinter(const FIDMap& FM, AnalyzerOptions &AnOpts,
               const Preprocessor &PP,
               const cross_tu::CrossTranslationUnitContext &CTU)
    : FM(FM), AnOpts(AnOpts), PP(PP), CTU(CTU) {
  }

  void ReportDiag(raw_ostream &o, const PathDiagnosticPiece& P) {
    ReportPiece(o, P, /*indent*/ 4, /*depth*/ 0, /*includeControlFlow*/ true);

    // Don't emit a warning about an unused private field.
    (void)AnOpts;
  }

  /// Print the expansions of the collected macro pieces.
  ///
  /// Each time ReportDiag is called on a PathDiagnosticMacroPiece (or, if one
  /// is found through a call piece, etc), it's subpieces are reported, and the
  /// piece itself is collected. Call this function after the entire bugpath
  /// was reported.
  void ReportMacroExpansions(raw_ostream &o, unsigned indent);

private:
  void ReportPiece(raw_ostream &o, const PathDiagnosticPiece &P,
                   unsigned indent, unsigned depth, bool includeControlFlow,
                   bool isKeyEvent = false) {
    switch (P.getKind()) {
      case PathDiagnosticPiece::ControlFlow:
        if (includeControlFlow)
          ReportControlFlow(o, cast<PathDiagnosticControlFlowPiece>(P), indent);
        break;
      case PathDiagnosticPiece::Call:
        ReportCall(o, cast<PathDiagnosticCallPiece>(P), indent,
                   depth);
        break;
      case PathDiagnosticPiece::Event:
        ReportEvent(o, cast<PathDiagnosticEventPiece>(P), indent, depth,
                    isKeyEvent);
        break;
      case PathDiagnosticPiece::Macro:
        ReportMacroSubPieces(o, cast<PathDiagnosticMacroPiece>(P), indent,
                             depth);
        break;
      case PathDiagnosticPiece::Note:
        ReportNote(o, cast<PathDiagnosticNotePiece>(P), indent);
        break;
      case PathDiagnosticPiece::PopUp:
        ReportPopUp(o, cast<PathDiagnosticPopUpPiece>(P), indent);
        break;
    }
  }

  void EmitRanges(raw_ostream &o, const ArrayRef<SourceRange> Ranges,
                  unsigned indent);
  void EmitMessage(raw_ostream &o, StringRef Message, unsigned indent);
  void EmitFixits(raw_ostream &o, ArrayRef<FixItHint> fixits, unsigned indent);

  void ReportControlFlow(raw_ostream &o,
                         const PathDiagnosticControlFlowPiece& P,
                         unsigned indent);
  void ReportEvent(raw_ostream &o, const PathDiagnosticEventPiece& P,
                   unsigned indent, unsigned depth, bool isKeyEvent = false);
  void ReportCall(raw_ostream &o, const PathDiagnosticCallPiece &P,
                  unsigned indent, unsigned depth);
  void ReportMacroSubPieces(raw_ostream &o, const PathDiagnosticMacroPiece& P,
                            unsigned indent, unsigned depth);
  void ReportNote(raw_ostream &o, const PathDiagnosticNotePiece& P,
                  unsigned indent);

  void ReportPopUp(raw_ostream &o, const PathDiagnosticPopUpPiece &P,
                   unsigned indent);
};

} // end of anonymous namespace

namespace {

struct ExpansionInfo {
  std::string MacroName;
  std::string Expansion;
  ExpansionInfo(std::string N, std::string E)
    : MacroName(std::move(N)), Expansion(std::move(E)) {}
};

} // end of anonymous namespace

static void printBugPath(llvm::raw_ostream &o, const FIDMap& FM,
                         AnalyzerOptions &AnOpts, const Preprocessor &PP,
                         const cross_tu::CrossTranslationUnitContext &CTU,
                         const PathPieces &Path);

/// Print coverage information to output stream {@code o}.
/// May modify the used list of files {@code Fids} by inserting new ones.
static void printCoverage(const PathDiagnostic *D,
                          unsigned InputIndentLevel,
                          SmallVectorImpl<FileID> &Fids,
                          FIDMap &FM,
                          llvm::raw_fd_ostream &o);

static ExpansionInfo
getExpandedMacro(SourceLocation MacroLoc, const Preprocessor &PP,
                 const cross_tu::CrossTranslationUnitContext &CTU);

//===----------------------------------------------------------------------===//
// Methods of PlistPrinter.
//===----------------------------------------------------------------------===//

void PlistPrinter::EmitRanges(raw_ostream &o,
                              const ArrayRef<SourceRange> Ranges,
                              unsigned indent) {

  if (Ranges.empty())
    return;

  Indent(o, indent) << "<key>ranges</key>\n";
  Indent(o, indent) << "<array>\n";
  ++indent;

  const SourceManager &SM = PP.getSourceManager();
  const LangOptions &LangOpts = PP.getLangOpts();

  for (auto &R : Ranges)
    EmitRange(o, SM,
              Lexer::getAsCharRange(SM.getExpansionRange(R), SM, LangOpts),
              FM, indent + 1);
  --indent;
  Indent(o, indent) << "</array>\n";
}

void PlistPrinter::EmitMessage(raw_ostream &o, StringRef Message,
                               unsigned indent) {
  // Output the text.
  assert(!Message.empty());
  Indent(o, indent) << "<key>extended_message</key>\n";
  Indent(o, indent);
  EmitString(o, Message) << '\n';

  // Output the short text.
  // FIXME: Really use a short string.
  Indent(o, indent) << "<key>message</key>\n";
  Indent(o, indent);
  EmitString(o, Message) << '\n';
}

void PlistPrinter::EmitFixits(raw_ostream &o, ArrayRef<FixItHint> fixits,
                              unsigned indent) {
  if (fixits.size() == 0)
    return;

  const SourceManager &SM = PP.getSourceManager();
  const LangOptions &LangOpts = PP.getLangOpts();

  Indent(o, indent) << "<key>fixits</key>\n";
  Indent(o, indent) << "<array>\n";
  for (const auto &fixit : fixits) {
    assert(!fixit.isNull());
    // FIXME: Add support for InsertFromRange and BeforePreviousInsertion.
    assert(!fixit.InsertFromRange.isValid() && "Not implemented yet!");
    assert(!fixit.BeforePreviousInsertions && "Not implemented yet!");
    Indent(o, indent) << " <dict>\n";
    Indent(o, indent) << "  <key>remove_range</key>\n";
    EmitRange(o, SM, Lexer::getAsCharRange(fixit.RemoveRange, SM, LangOpts),
              FM, indent + 2);
    Indent(o, indent) << "  <key>insert_string</key>";
    EmitString(o, fixit.CodeToInsert);
    o << "\n";
    Indent(o, indent) << " </dict>\n";
  }
  Indent(o, indent) << "</array>\n";
}

void PlistPrinter::ReportControlFlow(raw_ostream &o,
                                     const PathDiagnosticControlFlowPiece& P,
                                     unsigned indent) {

  const SourceManager &SM = PP.getSourceManager();
  const LangOptions &LangOpts = PP.getLangOpts();

  Indent(o, indent) << "<dict>\n";
  ++indent;

  Indent(o, indent) << "<key>kind</key><string>control</string>\n";

  // Emit edges.
  Indent(o, indent) << "<key>edges</key>\n";
  ++indent;
  Indent(o, indent) << "<array>\n";
  ++indent;
  for (PathDiagnosticControlFlowPiece::const_iterator I=P.begin(), E=P.end();
       I!=E; ++I) {
    Indent(o, indent) << "<dict>\n";
    ++indent;

    // Make the ranges of the start and end point self-consistent with adjacent edges
    // by forcing to use only the beginning of the range.  This simplifies the layout
    // logic for clients.
    Indent(o, indent) << "<key>start</key>\n";
    SourceRange StartEdge(
        SM.getExpansionLoc(I->getStart().asRange().getBegin()));
    EmitRange(o, SM, Lexer::getAsCharRange(StartEdge, SM, LangOpts), FM,
              indent + 1);

    Indent(o, indent) << "<key>end</key>\n";
    SourceRange EndEdge(SM.getExpansionLoc(I->getEnd().asRange().getBegin()));
    EmitRange(o, SM, Lexer::getAsCharRange(EndEdge, SM, LangOpts), FM,
              indent + 1);

    --indent;
    Indent(o, indent) << "</dict>\n";
  }
  --indent;
  Indent(o, indent) << "</array>\n";
  --indent;

  // Output any helper text.
  const auto &s = P.getString();
  if (!s.empty()) {
    Indent(o, indent) << "<key>alternate</key>";
    EmitString(o, s) << '\n';
  }

  assert(P.getFixits().size() == 0 &&
         "Fixits on constrol flow pieces are not implemented yet!");

  --indent;
  Indent(o, indent) << "</dict>\n";
}

void PlistPrinter::ReportEvent(raw_ostream &o, const PathDiagnosticEventPiece& P,
                               unsigned indent, unsigned depth,
                               bool isKeyEvent) {

  const SourceManager &SM = PP.getSourceManager();

  Indent(o, indent) << "<dict>\n";
  ++indent;

  Indent(o, indent) << "<key>kind</key><string>event</string>\n";

  if (isKeyEvent) {
    Indent(o, indent) << "<key>key_event</key><true/>\n";
  }

  // Output the location.
  FullSourceLoc L = P.getLocation().asLocation();

  Indent(o, indent) << "<key>location</key>\n";
  EmitLocation(o, SM, L, FM, indent);

  // Output the ranges (if any).
  ArrayRef<SourceRange> Ranges = P.getRanges();
  EmitRanges(o, Ranges, indent);

  // Output the call depth.
  Indent(o, indent) << "<key>depth</key>";
  EmitInteger(o, depth) << '\n';

  // Output the text.
  EmitMessage(o, P.getString(), indent);

  // Output the fixits.
  EmitFixits(o, P.getFixits(), indent);

  // Finish up.
  --indent;
  Indent(o, indent); o << "</dict>\n";
}

void PlistPrinter::ReportCall(raw_ostream &o, const PathDiagnosticCallPiece &P,
                              unsigned indent,
                              unsigned depth) {

  if (auto callEnter = P.getCallEnterEvent())
    ReportPiece(o, *callEnter, indent, depth, /*includeControlFlow*/ true,
                P.isLastInMainSourceFile());

  ++depth;

  if (auto callEnterWithinCaller = P.getCallEnterWithinCallerEvent())
    ReportPiece(o, *callEnterWithinCaller, indent, depth,
                /*includeControlFlow*/ true);

  for (PathPieces::const_iterator I = P.path.begin(), E = P.path.end();I!=E;++I)
    ReportPiece(o, **I, indent, depth, /*includeControlFlow*/ true);

  --depth;

  if (auto callExit = P.getCallExitEvent())
    ReportPiece(o, *callExit, indent, depth, /*includeControlFlow*/ true);

  assert(P.getFixits().size() == 0 &&
         "Fixits on call pieces are not implemented yet!");
}

void PlistPrinter::ReportMacroSubPieces(raw_ostream &o,
                                        const PathDiagnosticMacroPiece& P,
                                        unsigned indent, unsigned depth) {
  MacroPieces.push_back(&P);

  for (PathPieces::const_iterator I = P.subPieces.begin(),
                                  E = P.subPieces.end();
       I != E; ++I) {
    ReportPiece(o, **I, indent, depth, /*includeControlFlow*/ false);
  }

  assert(P.getFixits().size() == 0 &&
         "Fixits on constrol flow pieces are not implemented yet!");
}

void PlistPrinter::ReportMacroExpansions(raw_ostream &o, unsigned indent) {

  for (const PathDiagnosticMacroPiece *P : MacroPieces) {
    const SourceManager &SM = PP.getSourceManager();
    ExpansionInfo EI = getExpandedMacro(P->getLocation().asLocation(), PP, CTU);

    Indent(o, indent) << "<dict>\n";
    ++indent;

    // Output the location.
    FullSourceLoc L = P->getLocation().asLocation();

    Indent(o, indent) << "<key>location</key>\n";
    EmitLocation(o, SM, L, FM, indent);

    // Output the ranges (if any).
    ArrayRef<SourceRange> Ranges = P->getRanges();
    EmitRanges(o, Ranges, indent);

    // Output the macro name.
    Indent(o, indent) << "<key>name</key>";
    EmitString(o, EI.MacroName) << '\n';

    // Output what it expands into.
    Indent(o, indent) << "<key>expansion</key>";
    EmitString(o, EI.Expansion) << '\n';

    // Finish up.
    --indent;
    Indent(o, indent);
    o << "</dict>\n";
  }
}

void PlistPrinter::ReportNote(raw_ostream &o, const PathDiagnosticNotePiece& P,
                              unsigned indent) {

  const SourceManager &SM = PP.getSourceManager();

  Indent(o, indent) << "<dict>\n";
  ++indent;

  // Output the location.
  FullSourceLoc L = P.getLocation().asLocation();

  Indent(o, indent) << "<key>location</key>\n";
  EmitLocation(o, SM, L, FM, indent);

  // Output the ranges (if any).
  ArrayRef<SourceRange> Ranges = P.getRanges();
  EmitRanges(o, Ranges, indent);

  // Output the text.
  EmitMessage(o, P.getString(), indent);

  // Output the fixits.
  EmitFixits(o, P.getFixits(), indent);

  // Finish up.
  --indent;
  Indent(o, indent); o << "</dict>\n";
}

void PlistPrinter::ReportPopUp(raw_ostream &o,
                               const PathDiagnosticPopUpPiece &P,
                               unsigned indent) {
  const SourceManager &SM = PP.getSourceManager();

  Indent(o, indent) << "<dict>\n";
  ++indent;

  Indent(o, indent) << "<key>kind</key><string>pop-up</string>\n";

  // Output the location.
  FullSourceLoc L = P.getLocation().asLocation();

  Indent(o, indent) << "<key>location</key>\n";
  EmitLocation(o, SM, L, FM, indent);

  // Output the ranges (if any).
  ArrayRef<SourceRange> Ranges = P.getRanges();
  EmitRanges(o, Ranges, indent);

  // Output the text.
  EmitMessage(o, P.getString(), indent);

  assert(P.getFixits().size() == 0 &&
         "Fixits on pop-up pieces are not implemented yet!");

  // Finish up.
  --indent;
  Indent(o, indent) << "</dict>\n";
}

//===----------------------------------------------------------------------===//
// Static function definitions.
//===----------------------------------------------------------------------===//

/// Print coverage information to output stream {@code o}.
/// May modify the used list of files {@code Fids} by inserting new ones.
static void printCoverage(const PathDiagnostic *D,
                          unsigned InputIndentLevel,
                          SmallVectorImpl<FileID> &Fids,
                          FIDMap &FM,
                          llvm::raw_fd_ostream &o) {
  unsigned IndentLevel = InputIndentLevel;

  Indent(o, IndentLevel) << "<key>ExecutedLines</key>\n";
  Indent(o, IndentLevel) << "<dict>\n";
  IndentLevel++;

  // Mapping from file IDs to executed lines.
  const FilesToLineNumsMap &ExecutedLines = D->getExecutedLines();
  for (auto I = ExecutedLines.begin(), E = ExecutedLines.end(); I != E; ++I) {
    unsigned FileKey = AddFID(FM, Fids, I->first);
    Indent(o, IndentLevel) << "<key>" << FileKey << "</key>\n";
    Indent(o, IndentLevel) << "<array>\n";
    IndentLevel++;
    for (unsigned LineNo : I->second) {
      Indent(o, IndentLevel);
      EmitInteger(o, LineNo) << "\n";
    }
    IndentLevel--;
    Indent(o, IndentLevel) << "</array>\n";
  }
  IndentLevel--;
  Indent(o, IndentLevel) << "</dict>\n";

  assert(IndentLevel == InputIndentLevel);
}

static void printBugPath(llvm::raw_ostream &o, const FIDMap& FM,
                         AnalyzerOptions &AnOpts, const Preprocessor &PP,
                         const cross_tu::CrossTranslationUnitContext &CTU,
                         const PathPieces &Path) {
  PlistPrinter Printer(FM, AnOpts, PP, CTU);
  assert(std::is_partitioned(Path.begin(), Path.end(),
                             [](const PathDiagnosticPieceRef &E) {
                               return E->getKind() == PathDiagnosticPiece::Note;
                             }) &&
         "PathDiagnostic is not partitioned so that notes precede the rest");

  PathPieces::const_iterator FirstNonNote = std::partition_point(
      Path.begin(), Path.end(), [](const PathDiagnosticPieceRef &E) {
        return E->getKind() == PathDiagnosticPiece::Note;
      });

  PathPieces::const_iterator I = Path.begin();

  if (FirstNonNote != Path.begin()) {
    o << "   <key>notes</key>\n"
         "   <array>\n";

    for (; I != FirstNonNote; ++I)
      Printer.ReportDiag(o, **I);

    o << "   </array>\n";
  }

  o << "   <key>path</key>\n";

  o << "   <array>\n";

  for (PathPieces::const_iterator E = Path.end(); I != E; ++I)
    Printer.ReportDiag(o, **I);

  o << "   </array>\n";

  if (!AnOpts.ShouldDisplayMacroExpansions)
    return;

  o << "   <key>macro_expansions</key>\n"
       "   <array>\n";
  Printer.ReportMacroExpansions(o, /* indent */ 4);
  o << "   </array>\n";
}

//===----------------------------------------------------------------------===//
// Methods of PlistDiagnostics.
//===----------------------------------------------------------------------===//

PlistDiagnostics::PlistDiagnostics(
    AnalyzerOptions &AnalyzerOpts, const std::string &output,
    const Preprocessor &PP, const cross_tu::CrossTranslationUnitContext &CTU,
    bool supportsMultipleFiles)
    : OutputFile(output), PP(PP), CTU(CTU), AnOpts(AnalyzerOpts),
      SupportsCrossFileDiagnostics(supportsMultipleFiles) {
  // FIXME: Will be used by a later planned change.
  (void)this->CTU;
}

void ento::createPlistDiagnosticConsumer(
    AnalyzerOptions &AnalyzerOpts, PathDiagnosticConsumers &C,
    const std::string &s, const Preprocessor &PP,
    const cross_tu::CrossTranslationUnitContext &CTU) {
  C.push_back(new PlistDiagnostics(AnalyzerOpts, s, PP, CTU,
                                   /*supportsMultipleFiles*/ false));
}

void ento::createPlistMultiFileDiagnosticConsumer(
    AnalyzerOptions &AnalyzerOpts, PathDiagnosticConsumers &C,
    const std::string &s, const Preprocessor &PP,
    const cross_tu::CrossTranslationUnitContext &CTU) {
  C.push_back(new PlistDiagnostics(AnalyzerOpts, s, PP, CTU,
                                   /*supportsMultipleFiles*/ true));
}
void PlistDiagnostics::FlushDiagnosticsImpl(
                                    std::vector<const PathDiagnostic *> &Diags,
                                    FilesMade *filesMade) {
  // Build up a set of FIDs that we use by scanning the locations and
  // ranges of the diagnostics.
  FIDMap FM;
  SmallVector<FileID, 10> Fids;
  const SourceManager& SM = PP.getSourceManager();
  const LangOptions &LangOpts = PP.getLangOpts();

  auto AddPieceFID = [&FM, &Fids, &SM](const PathDiagnosticPiece &Piece) {
    AddFID(FM, Fids, SM, Piece.getLocation().asLocation());
    ArrayRef<SourceRange> Ranges = Piece.getRanges();
    for (const SourceRange &Range : Ranges) {
      AddFID(FM, Fids, SM, Range.getBegin());
      AddFID(FM, Fids, SM, Range.getEnd());
    }
  };

  for (const PathDiagnostic *D : Diags) {

    SmallVector<const PathPieces *, 5> WorkList;
    WorkList.push_back(&D->path);

    while (!WorkList.empty()) {
      const PathPieces &Path = *WorkList.pop_back_val();

      for (const auto &Iter : Path) {
        const PathDiagnosticPiece &Piece = *Iter;
        AddPieceFID(Piece);

        if (const PathDiagnosticCallPiece *Call =
                dyn_cast<PathDiagnosticCallPiece>(&Piece)) {
          if (auto CallEnterWithin = Call->getCallEnterWithinCallerEvent())
            AddPieceFID(*CallEnterWithin);

          if (auto CallEnterEvent = Call->getCallEnterEvent())
            AddPieceFID(*CallEnterEvent);

          WorkList.push_back(&Call->path);
        } else if (const PathDiagnosticMacroPiece *Macro =
                       dyn_cast<PathDiagnosticMacroPiece>(&Piece)) {
          WorkList.push_back(&Macro->subPieces);
        }
      }
    }
  }

  // Open the file.
  std::error_code EC;
  llvm::raw_fd_ostream o(OutputFile, EC, llvm::sys::fs::OF_Text);
  if (EC) {
    llvm::errs() << "warning: could not create file: " << EC.message() << '\n';
    return;
  }

  EmitPlistHeader(o);

  // Write the root object: a <dict> containing...
  //  - "clang_version", the string representation of clang version
  //  - "files", an <array> mapping from FIDs to file names
  //  - "diagnostics", an <array> containing the path diagnostics
  o << "<dict>\n" <<
       " <key>clang_version</key>\n";
  EmitString(o, getClangFullVersion()) << '\n';
  o << " <key>diagnostics</key>\n"
       " <array>\n";

  for (std::vector<const PathDiagnostic*>::iterator DI=Diags.begin(),
       DE = Diags.end(); DI!=DE; ++DI) {

    o << "  <dict>\n";

    const PathDiagnostic *D = *DI;
    printBugPath(o, FM, AnOpts, PP, CTU, D->path);

    // Output the bug type and bug category.
    o << "   <key>description</key>";
    EmitString(o, D->getShortDescription()) << '\n';
    o << "   <key>category</key>";
    EmitString(o, D->getCategory()) << '\n';
    o << "   <key>type</key>";
    EmitString(o, D->getBugType()) << '\n';
    o << "   <key>check_name</key>";
    EmitString(o, D->getCheckerName()) << '\n';

    o << "   <!-- This hash is experimental and going to change! -->\n";
    o << "   <key>issue_hash_content_of_line_in_context</key>";
    PathDiagnosticLocation UPDLoc = D->getUniqueingLoc();
    FullSourceLoc L(SM.getExpansionLoc(UPDLoc.isValid()
                                            ? UPDLoc.asLocation()
                                            : D->getLocation().asLocation()),
                    SM);
    const Decl *DeclWithIssue = D->getDeclWithIssue();
    EmitString(o, GetIssueHash(SM, L, D->getCheckerName(), D->getBugType(),
                               DeclWithIssue, LangOpts))
        << '\n';

    // Output information about the semantic context where
    // the issue occurred.
    if (const Decl *DeclWithIssue = D->getDeclWithIssue()) {
      // FIXME: handle blocks, which have no name.
      if (const NamedDecl *ND = dyn_cast<NamedDecl>(DeclWithIssue)) {
        StringRef declKind;
        switch (ND->getKind()) {
          case Decl::CXXRecord:
            declKind = "C++ class";
            break;
          case Decl::CXXMethod:
            declKind = "C++ method";
            break;
          case Decl::ObjCMethod:
            declKind = "Objective-C method";
            break;
          case Decl::Function:
            declKind = "function";
            break;
          default:
            break;
        }
        if (!declKind.empty()) {
          const std::string &declName = ND->getDeclName().getAsString();
          o << "  <key>issue_context_kind</key>";
          EmitString(o, declKind) << '\n';
          o << "  <key>issue_context</key>";
          EmitString(o, declName) << '\n';
        }

        // Output the bug hash for issue unique-ing. Currently, it's just an
        // offset from the beginning of the function.
        if (const Stmt *Body = DeclWithIssue->getBody()) {

          // If the bug uniqueing location exists, use it for the hash.
          // For example, this ensures that two leaks reported on the same line
          // will have different issue_hashes and that the hash will identify
          // the leak location even after code is added between the allocation
          // site and the end of scope (leak report location).
          if (UPDLoc.isValid()) {
            FullSourceLoc UFunL(
                SM.getExpansionLoc(
                    D->getUniqueingDecl()->getBody()->getBeginLoc()),
                SM);
            o << "  <key>issue_hash_function_offset</key><string>"
              << L.getExpansionLineNumber() - UFunL.getExpansionLineNumber()
              << "</string>\n";

          // Otherwise, use the location on which the bug is reported.
          } else {
            FullSourceLoc FunL(SM.getExpansionLoc(Body->getBeginLoc()), SM);
            o << "  <key>issue_hash_function_offset</key><string>"
              << L.getExpansionLineNumber() - FunL.getExpansionLineNumber()
              << "</string>\n";
          }

        }
      }
    }

    // Output the location of the bug.
    o << "  <key>location</key>\n";
    EmitLocation(o, SM, D->getLocation().asLocation(), FM, 2);

    // Output the diagnostic to the sub-diagnostic client, if any.
    if (!filesMade->empty()) {
      StringRef lastName;
      PDFileEntry::ConsumerFiles *files = filesMade->getFiles(*D);
      if (files) {
        for (PDFileEntry::ConsumerFiles::const_iterator CI = files->begin(),
                CE = files->end(); CI != CE; ++CI) {
          StringRef newName = CI->first;
          if (newName != lastName) {
            if (!lastName.empty()) {
              o << "  </array>\n";
            }
            lastName = newName;
            o <<  "  <key>" << lastName << "_files</key>\n";
            o << "  <array>\n";
          }
          o << "   <string>" << CI->second << "</string>\n";
        }
        o << "  </array>\n";
      }
    }

    printCoverage(D, /*IndentLevel=*/2, Fids, FM, o);

    // Close up the entry.
    o << "  </dict>\n";
  }

  o << " </array>\n";

  o << " <key>files</key>\n"
       " <array>\n";
  for (FileID FID : Fids)
    EmitString(o << "  ", SM.getFileEntryForID(FID)->getName()) << '\n';
  o << " </array>\n";

  if (llvm::AreStatisticsEnabled() && AnOpts.ShouldSerializeStats) {
    o << " <key>statistics</key>\n";
    std::string stats;
    llvm::raw_string_ostream os(stats);
    llvm::PrintStatisticsJSON(os);
    os.flush();
    EmitString(o, html::EscapeText(stats)) << '\n';
  }

  // Finish.
  o << "</dict>\n</plist>\n";
}

//===----------------------------------------------------------------------===//
// Declarations of helper functions and data structures for expanding macros.
//===----------------------------------------------------------------------===//

namespace {

using ExpArgTokens = llvm::SmallVector<Token, 2>;

/// Maps unexpanded macro arguments to expanded arguments. A macro argument may
/// need to expanded further when it is nested inside another macro.
class MacroArgMap : public std::map<const IdentifierInfo *, ExpArgTokens> {
public:
  void expandFromPrevMacro(const MacroArgMap &Super);
};

struct MacroNameAndArgs {
  std::string Name;
  const MacroInfo *MI = nullptr;
  MacroArgMap Args;

  MacroNameAndArgs(std::string N, const MacroInfo *MI, MacroArgMap M)
    : Name(std::move(N)), MI(MI), Args(std::move(M)) {}
};

class TokenPrinter {
  llvm::raw_ostream &OS;
  const Preprocessor &PP;

  Token PrevTok, PrevPrevTok;
  TokenConcatenation ConcatInfo;

public:
  TokenPrinter(llvm::raw_ostream &OS, const Preprocessor &PP)
    : OS(OS), PP(PP), ConcatInfo(PP) {
    PrevTok.setKind(tok::unknown);
    PrevPrevTok.setKind(tok::unknown);
  }

  void printToken(const Token &Tok);
};

} // end of anonymous namespace

/// The implementation method of getMacroExpansion: It prints the expansion of
/// a macro to \p Printer, and returns with the name of the macro.
///
/// Since macros can be nested in one another, this function may call itself
/// recursively.
///
/// Unfortunately, macro arguments have to expanded manually. To understand why,
/// observe the following example:
///
///   #define PRINT(x) print(x)
///   #define DO_SOMETHING(str) PRINT(str)
///
///   DO_SOMETHING("Cute panda cubs.");
///
/// As we expand the last line, we'll immediately replace PRINT(str) with
/// print(x). The information that both 'str' and 'x' refers to the same string
/// is an information we have to forward, hence the argument \p PrevArgs.
///
/// To avoid infinite recursion we maintain the already processed tokens in
/// a set. This is carried as a parameter through the recursive calls. The set
/// is extended with the currently processed token and after processing it, the
/// token is removed. If the token is already in the set, then recursion stops:
///
/// #define f(y) x
/// #define x f(x)
static std::string getMacroNameAndPrintExpansion(
    TokenPrinter &Printer,
    SourceLocation MacroLoc,
    const Preprocessor &PP,
    const MacroArgMap &PrevArgs,
    llvm::SmallPtrSet<IdentifierInfo *, 8> &AlreadyProcessedTokens);

/// Retrieves the name of the macro and what it's arguments expand into
/// at \p ExpanLoc.
///
/// For example, for the following macro expansion:
///
///   #define SET_TO_NULL(x) x = 0
///   #define NOT_SUSPICIOUS(a) \
///     {                       \
///       int b = 0;            \
///     }                       \
///     SET_TO_NULL(a)
///
///   int *ptr = new int(4);
///   NOT_SUSPICIOUS(&ptr);
///   *ptr = 5;
///
/// When \p ExpanLoc references the last line, the macro name "NOT_SUSPICIOUS"
/// and the MacroArgMap map { (a, &ptr) } will be returned.
///
/// When \p ExpanLoc references "SET_TO_NULL(a)" within the definition of
/// "NOT_SUSPICOUS", the macro name "SET_TO_NULL" and the MacroArgMap map
/// { (x, a) } will be returned.
static MacroNameAndArgs getMacroNameAndArgs(SourceLocation ExpanLoc,
                                            const Preprocessor &PP);

/// Retrieves the ')' token that matches '(' \p It points to.
static MacroInfo::tokens_iterator getMatchingRParen(
    MacroInfo::tokens_iterator It,
    MacroInfo::tokens_iterator End);

/// Retrieves the macro info for \p II refers to at \p Loc. This is important
/// because macros can be redefined or undefined.
static const MacroInfo *getMacroInfoForLocation(const Preprocessor &PP,
                                                const SourceManager &SM,
                                                const IdentifierInfo *II,
                                                SourceLocation Loc);

//===----------------------------------------------------------------------===//
// Definitions of helper functions and methods for expanding macros.
//===----------------------------------------------------------------------===//

static ExpansionInfo
getExpandedMacro(SourceLocation MacroLoc, const Preprocessor &PP,
                 const cross_tu::CrossTranslationUnitContext &CTU) {

  const Preprocessor *PPToUse = &PP;
  if (auto LocAndUnit = CTU.getImportedFromSourceLocation(MacroLoc)) {
    MacroLoc = LocAndUnit->first;
    PPToUse = &LocAndUnit->second->getPreprocessor();
  }

  llvm::SmallString<200> ExpansionBuf;
  llvm::raw_svector_ostream OS(ExpansionBuf);
  TokenPrinter Printer(OS, *PPToUse);
  llvm::SmallPtrSet<IdentifierInfo*, 8> AlreadyProcessedTokens;

  std::string MacroName = getMacroNameAndPrintExpansion(
      Printer, MacroLoc, *PPToUse, MacroArgMap{}, AlreadyProcessedTokens);
  return { MacroName, OS.str() };
}

static std::string getMacroNameAndPrintExpansion(
    TokenPrinter &Printer,
    SourceLocation MacroLoc,
    const Preprocessor &PP,
    const MacroArgMap &PrevArgs,
    llvm::SmallPtrSet<IdentifierInfo *, 8> &AlreadyProcessedTokens) {

  const SourceManager &SM = PP.getSourceManager();

  MacroNameAndArgs Info = getMacroNameAndArgs(SM.getExpansionLoc(MacroLoc), PP);
  IdentifierInfo* IDInfo = PP.getIdentifierInfo(Info.Name);

  // TODO: If the macro definition contains another symbol then this function is
  // called recursively. In case this symbol is the one being defined, it will
  // be an infinite recursion which is stopped by this "if" statement. However,
  // in this case we don't get the full expansion text in the Plist file. See
  // the test file where "value" is expanded to "garbage_" instead of
  // "garbage_value".
  if (AlreadyProcessedTokens.find(IDInfo) != AlreadyProcessedTokens.end())
    return Info.Name;
  AlreadyProcessedTokens.insert(IDInfo);

  if (!Info.MI)
    return Info.Name;

  // Manually expand its arguments from the previous macro.
  Info.Args.expandFromPrevMacro(PrevArgs);

  // Iterate over the macro's tokens and stringify them.
  for (auto It = Info.MI->tokens_begin(), E = Info.MI->tokens_end(); It != E;
       ++It) {
    Token T = *It;

    // If this token is not an identifier, we only need to print it.
    if (T.isNot(tok::identifier)) {
      Printer.printToken(T);
      continue;
    }

    const auto *II = T.getIdentifierInfo();
    assert(II &&
          "This token is an identifier but has no IdentifierInfo!");

    // If this token is a macro that should be expanded inside the current
    // macro.
    if (getMacroInfoForLocation(PP, SM, II, T.getLocation())) {
      getMacroNameAndPrintExpansion(Printer, T.getLocation(), PP, Info.Args,
                                    AlreadyProcessedTokens);

      // If this is a function-like macro, skip its arguments, as
      // getExpandedMacro() already printed them. If this is the case, let's
      // first jump to the '(' token.
      auto N = std::next(It);
      if (N != E && N->is(tok::l_paren))
        It = getMatchingRParen(++It, E);
      continue;
    }

    // If this token is the current macro's argument, we should expand it.
    auto ArgMapIt = Info.Args.find(II);
    if (ArgMapIt != Info.Args.end()) {
      for (MacroInfo::tokens_iterator ArgIt = ArgMapIt->second.begin(),
                                      ArgEnd = ArgMapIt->second.end();
           ArgIt != ArgEnd; ++ArgIt) {

        // These tokens may still be macros, if that is the case, handle it the
        // same way we did above.
        const auto *ArgII = ArgIt->getIdentifierInfo();
        if (!ArgII) {
          Printer.printToken(*ArgIt);
          continue;
        }

        const auto *MI = PP.getMacroInfo(ArgII);
        if (!MI) {
          Printer.printToken(*ArgIt);
          continue;
        }

        getMacroNameAndPrintExpansion(Printer, ArgIt->getLocation(), PP,
                                      Info.Args, AlreadyProcessedTokens);
        // Peek the next token if it is a tok::l_paren. This way we can decide
        // if this is the application or just a reference to a function maxro
        // symbol:
        //
        // #define apply(f) ...
        // #define func(x) ...
        // apply(func)
        // apply(func(42))
        auto N = std::next(ArgIt);
        if (N != ArgEnd && N->is(tok::l_paren))
          ArgIt = getMatchingRParen(++ArgIt, ArgEnd);
      }
      continue;
    }

    // If control reached here, then this token isn't a macro identifier, nor an
    // unexpanded macro argument that we need to handle, print it.
    Printer.printToken(T);
  }

  AlreadyProcessedTokens.erase(IDInfo);

  return Info.Name;
}

static MacroNameAndArgs getMacroNameAndArgs(SourceLocation ExpanLoc,
                                            const Preprocessor &PP) {

  const SourceManager &SM = PP.getSourceManager();
  const LangOptions &LangOpts = PP.getLangOpts();

  // First, we create a Lexer to lex *at the expansion location* the tokens
  // referring to the macro's name and its arguments.
  std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(ExpanLoc);
  const llvm::MemoryBuffer *MB = SM.getBuffer(LocInfo.first);
  const char *MacroNameTokenPos = MB->getBufferStart() + LocInfo.second;

  Lexer RawLexer(SM.getLocForStartOfFile(LocInfo.first), LangOpts,
                 MB->getBufferStart(), MacroNameTokenPos, MB->getBufferEnd());

  // Acquire the macro's name.
  Token TheTok;
  RawLexer.LexFromRawLexer(TheTok);

  std::string MacroName = PP.getSpelling(TheTok);

  const auto *II = PP.getIdentifierInfo(MacroName);
  assert(II && "Failed to acquire the IndetifierInfo for the macro!");

  const MacroInfo *MI = getMacroInfoForLocation(PP, SM, II, ExpanLoc);
  // assert(MI && "The macro must've been defined at it's expansion location!");
  //
  // We should always be able to obtain the MacroInfo in a given TU, but if
  // we're running the analyzer with CTU, the Preprocessor won't contain the
  // directive history (or anything for that matter) from another TU.
  // TODO: assert when we're not running with CTU.
  if (!MI)
    return { MacroName, MI, {} };

  // Acquire the macro's arguments.
  //
  // The rough idea here is to lex from the first left parentheses to the last
  // right parentheses, and map the macro's unexpanded arguments to what they
  // will be expanded to. An expanded macro argument may contain several tokens
  // (like '3 + 4'), so we'll lex until we find a tok::comma or tok::r_paren, at
  // which point we start lexing the next argument or finish.
  ArrayRef<const IdentifierInfo *> MacroArgs = MI->params();
  if (MacroArgs.empty())
    return { MacroName, MI, {} };

  RawLexer.LexFromRawLexer(TheTok);
  // When this is a token which expands to another macro function then its
  // parentheses are not at its expansion locaiton. For example:
  //
  // #define foo(x) int bar() { return x; }
  // #define apply_zero(f) f(0)
  // apply_zero(foo)
  //               ^
  //               This is not a tok::l_paren, but foo is a function.
  if (TheTok.isNot(tok::l_paren))
    return { MacroName, MI, {} };

  MacroArgMap Args;

  // When the macro's argument is a function call, like
  //   CALL_FN(someFunctionName(param1, param2))
  // we will find tok::l_paren, tok::r_paren, and tok::comma that do not divide
  // actual macro arguments, or do not represent the macro argument's closing
  // parentheses, so we'll count how many parentheses aren't closed yet.
  // If ParanthesesDepth
  //   * = 0, then there are no more arguments to lex.
  //   * = 1, then if we find a tok::comma, we can start lexing the next arg.
  //   * > 1, then tok::comma is a part of the current arg.
  int ParenthesesDepth = 1;

  // If we encounter __VA_ARGS__, we will lex until the closing tok::r_paren,
  // even if we lex a tok::comma and ParanthesesDepth == 1.
  const IdentifierInfo *__VA_ARGS__II = PP.getIdentifierInfo("__VA_ARGS__");

  for (const IdentifierInfo *UnexpArgII : MacroArgs) {
    MacroArgMap::mapped_type ExpandedArgTokens;

    // One could also simply not supply a single argument to __VA_ARGS__ -- this
    // results in a preprocessor warning, but is not an error:
    //   #define VARIADIC(ptr, ...) \
    //     someVariadicTemplateFunction(__VA_ARGS__)
    //
    //   int *ptr;
    //   VARIADIC(ptr); // Note that there are no commas, this isn't just an
    //                  // empty parameter -- there are no parameters for '...'.
    // In any other case, ParenthesesDepth mustn't be 0 here.
    if (ParenthesesDepth != 0) {

      // Lex the first token of the next macro parameter.
      RawLexer.LexFromRawLexer(TheTok);

      while (!(ParenthesesDepth == 1 &&
              (UnexpArgII == __VA_ARGS__II ? false : TheTok.is(tok::comma)))) {
        assert(TheTok.isNot(tok::eof) &&
               "EOF encountered while looking for expanded macro args!");

        if (TheTok.is(tok::l_paren))
          ++ParenthesesDepth;

        if (TheTok.is(tok::r_paren))
          --ParenthesesDepth;

        if (ParenthesesDepth == 0)
          break;

        if (TheTok.is(tok::raw_identifier))
          PP.LookUpIdentifierInfo(TheTok);

        ExpandedArgTokens.push_back(TheTok);
        RawLexer.LexFromRawLexer(TheTok);
      }
    } else {
      assert(UnexpArgII == __VA_ARGS__II);
    }

    Args.emplace(UnexpArgII, std::move(ExpandedArgTokens));
  }

  assert(TheTok.is(tok::r_paren) &&
         "Expanded macro argument acquisition failed! After the end of the loop"
         " this token should be ')'!");

  return { MacroName, MI, Args };
}

static MacroInfo::tokens_iterator getMatchingRParen(
    MacroInfo::tokens_iterator It,
    MacroInfo::tokens_iterator End) {

  assert(It->is(tok::l_paren) && "This token should be '('!");

  // Skip until we find the closing ')'.
  int ParenthesesDepth = 1;
  while (ParenthesesDepth != 0) {
    ++It;

    assert(It->isNot(tok::eof) &&
           "Encountered EOF while attempting to skip macro arguments!");
    assert(It != End &&
           "End of the macro definition reached before finding ')'!");

    if (It->is(tok::l_paren))
      ++ParenthesesDepth;

    if (It->is(tok::r_paren))
      --ParenthesesDepth;
  }
  return It;
}

static const MacroInfo *getMacroInfoForLocation(const Preprocessor &PP,
                                                const SourceManager &SM,
                                                const IdentifierInfo *II,
                                                SourceLocation Loc) {

  const MacroDirective *MD = PP.getLocalMacroDirectiveHistory(II);
  if (!MD)
    return nullptr;

  return MD->findDirectiveAtLoc(Loc, SM).getMacroInfo();
}

void MacroArgMap::expandFromPrevMacro(const MacroArgMap &Super) {

  for (value_type &Pair : *this) {
    ExpArgTokens &CurrExpArgTokens = Pair.second;

    // For each token in the expanded macro argument.
    auto It = CurrExpArgTokens.begin();
    while (It != CurrExpArgTokens.end()) {
      if (It->isNot(tok::identifier)) {
        ++It;
        continue;
      }

      const auto *II = It->getIdentifierInfo();
      assert(II);

      // Is this an argument that "Super" expands further?
      if (!Super.count(II)) {
        ++It;
        continue;
      }

      const ExpArgTokens &SuperExpArgTokens = Super.at(II);

      It = CurrExpArgTokens.insert(
          It, SuperExpArgTokens.begin(), SuperExpArgTokens.end());
      std::advance(It, SuperExpArgTokens.size());
      It = CurrExpArgTokens.erase(It);
    }
  }
}

void TokenPrinter::printToken(const Token &Tok) {
  // If this is the first token to be printed, don't print space.
  if (PrevTok.isNot(tok::unknown)) {
    // If the tokens were already space separated, or if they must be to avoid
    // them being implicitly pasted, add a space between them.
    if(Tok.hasLeadingSpace() || ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok,
                                                       Tok)) {
      // AvoidConcat doesn't check for ##, don't print a space around it.
      if (PrevTok.isNot(tok::hashhash) && Tok.isNot(tok::hashhash)) {
        OS << ' ';
      }
    }
  }

  if (!Tok.isOneOf(tok::hash, tok::hashhash)) {
    if (PrevTok.is(tok::hash))
      OS << '\"' << PP.getSpelling(Tok) << '\"';
    else
      OS << PP.getSpelling(Tok);
  }

  PrevPrevTok = PrevTok;
  PrevTok = Tok;
}


text:
declare module WinJSContrib.DataContainer {
	export var current: IDataContainer;

	interface IDataContainer {
        read<T>(itemkey: string): WinJS.Promise<T>;
        save(itemkey, obj): WinJS.Promise<any>;
        remove(itemkey): WinJS.Promise<any>;
        listKeys(): WinJS.Promise<string[]>;
        child(key): IDataContainer;
        childWithTransaction(key: string, process: (arg: IDataContainer) => WinJS.Promise<any>): WinJS.Promise<IDataContainer>;
        deleteContainer(): WinJS.Promise<any>;
	}
}


text:
using pb = global::Google.Protobuf;
using pbc = global::Google.Protobuf.Collections;
using pbr = global::Google.Protobuf.Reflection;
using scg = global::System.Collections.Generic;
namespace HOLMS.Types.OpExLogging {

  /// Holder for reflection information generated from opex_logging/operational_exception_notification.proto
  public static partial class OperationalExceptionNotificationReflection {

    #region Descriptor
    /// File descriptor for opex_logging/operational_exception_notification.proto
    public static pbr::FileDescriptor Descriptor {
      get { return descriptor; }
    }
    private static pbr::FileDescriptor descriptor;

    static OperationalExceptionNotificationReflection() {
      byte[] descriptorData = global::System.Convert.FromBase64String(
          string.Concat(
            "CjVvcGV4X2xvZ2dpbmcvb3BlcmF0aW9uYWxfZXhjZXB0aW9uX25vdGlmaWNh",
            "dGlvbi5wcm90bxIYaG9sbXMudHlwZXMub3BleF9sb2dnaW5nGiBpYW0vc3Rh",
            "ZmZfbWVtYmVyX2luZGljYXRvci5wcm90byKbAQogT3BlcmF0aW9uYWxFeGNl",
            "cHRpb25Ob3RpZmljYXRpb24SMwoEdXNlchgBIAEoCzIlLmhvbG1zLnR5cGVz",
            "LmlhbS5TdGFmZk1lbWJlckluZGljYXRvchIZChFleGNlcHRpb25fbWVzc2Fn",
            "ZRgCIAEoCRIUCgxleGNlcHRpb25faWQYAyABKAkSEQoJal93X3Rva2VuGAQg",
            "ASgJQhqqAhdIT0xNUy5UeXBlcy5PcEV4TG9nZ2luZ2IGcHJvdG8z"));
      descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData,
          new pbr::FileDescriptor[] { global::HOLMS.Types.IAM.StaffMemberIndicatorReflection.Descriptor, },
          new pbr::GeneratedClrTypeInfo(null, new pbr::GeneratedClrTypeInfo[] {
            new pbr::GeneratedClrTypeInfo(typeof(global::HOLMS.Types.OpExLogging.OperationalExceptionNotification), global::HOLMS.Types.OpExLogging.OperationalExceptionNotification.Parser, new[]{ "User", "ExceptionMessage", "ExceptionId", "JWToken" }, null, null, null)
          }));
    }
    #endregion

  }
  #region Messages
  public sealed partial class OperationalExceptionNotification : pb::IMessage<OperationalExceptionNotification> {
    private static readonly pb::MessageParser<OperationalExceptionNotification> _parser = new pb::MessageParser<OperationalExceptionNotification>(() => new OperationalExceptionNotification());
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    public static pb::MessageParser<OperationalExceptionNotification> Parser { get { return _parser; } }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    public static pbr::MessageDescriptor Descriptor {
      get { return global::HOLMS.Types.OpExLogging.OperationalExceptionNotificationReflection.Descriptor.MessageTypes[0]; }
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    pbr::MessageDescriptor pb::IMessage.Descriptor {
      get { return Descriptor; }
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    public OperationalExceptionNotification() {
      OnConstruction();
    }

    partial void OnConstruction();

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    public OperationalExceptionNotification(OperationalExceptionNotification other) : this() {
      User = other.user_ != null ? other.User.Clone() : null;
      exceptionMessage_ = other.exceptionMessage_;
      exceptionId_ = other.exceptionId_;
      jWToken_ = other.jWToken_;
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    public OperationalExceptionNotification Clone() {
      return new OperationalExceptionNotification(this);
    }

    /// Field number for the "user" field.
    public const int UserFieldNumber = 1;
    private global::HOLMS.Types.IAM.StaffMemberIndicator user_;
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    public global::HOLMS.Types.IAM.StaffMemberIndicator User {
      get { return user_; }
      set {
        user_ = value;
      }
    }

    /// Field number for the "exception_message" field.
    public const int ExceptionMessageFieldNumber = 2;
    private string exceptionMessage_ = "";
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    public string ExceptionMessage {
      get { return exceptionMessage_; }
      set {
        exceptionMessage_ = pb::ProtoPreconditions.CheckNotNull(value, "value");
      }
    }

    /// Field number for the "exception_id" field.
    public const int ExceptionIdFieldNumber = 3;
    private string exceptionId_ = "";
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    public string ExceptionId {
      get { return exceptionId_; }
      set {
        exceptionId_ = pb::ProtoPreconditions.CheckNotNull(value, "value");
      }
    }

    /// Field number for the "j_w_token" field.
    public const int JWTokenFieldNumber = 4;
    private string jWToken_ = "";
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    public string JWToken {
      get { return jWToken_; }
      set {
        jWToken_ = pb::ProtoPreconditions.CheckNotNull(value, "value");
      }
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    public override bool Equals(object other) {
      return Equals(other as OperationalExceptionNotification);
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    public bool Equals(OperationalExceptionNotification other) {
      if (ReferenceEquals(other, null)) {
        return false;
      }
      if (ReferenceEquals(other, this)) {
        return true;
      }
      if (!object.Equals(User, other.User)) return false;
      if (ExceptionMessage != other.ExceptionMessage) return false;
      if (ExceptionId != other.ExceptionId) return false;
      if (JWToken != other.JWToken) return false;
      return true;
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    public override int GetHashCode() {
      int hash = 1;
      if (user_ != null) hash ^= User.GetHashCode();
      if (ExceptionMessage.Length != 0) hash ^= ExceptionMessage.GetHashCode();
      if (ExceptionId.Length != 0) hash ^= ExceptionId.GetHashCode();
      if (JWToken.Length != 0) hash ^= JWToken.GetHashCode();
      return hash;
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    public override string ToString() {
      return pb::JsonFormatter.ToDiagnosticString(this);
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    public void WriteTo(pb::CodedOutputStream output) {
      if (user_ != null) {
        output.WriteRawTag(10);
        output.WriteMessage(User);
      }
      if (ExceptionMessage.Length != 0) {
        output.WriteRawTag(18);
        output.WriteString(ExceptionMessage);
      }
      if (ExceptionId.Length != 0) {
        output.WriteRawTag(26);
        output.WriteString(ExceptionId);
      }
      if (JWToken.Length != 0) {
        output.WriteRawTag(34);
        output.WriteString(JWToken);
      }
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    public int CalculateSize() {
      int size = 0;
      if (user_ != null) {
        size += 1 + pb::CodedOutputStream.ComputeMessageSize(User);
      }
      if (ExceptionMessage.Length != 0) {
        size += 1 + pb::CodedOutputStream.ComputeStringSize(ExceptionMessage);
      }
      if (ExceptionId.Length != 0) {
        size += 1 + pb::CodedOutputStream.ComputeStringSize(ExceptionId);
      }
      if (JWToken.Length != 0) {
        size += 1 + pb::CodedOutputStream.ComputeStringSize(JWToken);
      }
      return size;
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    public void MergeFrom(OperationalExceptionNotification other) {
      if (other == null) {
        return;
      }
      if (other.user_ != null) {
        if (user_ == null) {
          user_ = new global::HOLMS.Types.IAM.StaffMemberIndicator();
        }
        User.MergeFrom(other.User);
      }
      if (other.ExceptionMessage.Length != 0) {
        ExceptionMessage = other.ExceptionMessage;
      }
      if (other.ExceptionId.Length != 0) {
        ExceptionId = other.ExceptionId;
      }
      if (other.JWToken.Length != 0) {
        JWToken = other.JWToken;
      }
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    public void MergeFrom(pb::CodedInputStream input) {
      uint tag;
      while ((tag = input.ReadTag()) != 0) {
        switch(tag) {
          default:
            input.SkipLastField();
            break;
          case 10: {
            if (user_ == null) {
              user_ = new global::HOLMS.Types.IAM.StaffMemberIndicator();
            }
            input.ReadMessage(user_);
            break;
          }
          case 18: {
            ExceptionMessage = input.ReadString();
            break;
          }
          case 26: {
            ExceptionId = input.ReadString();
            break;
          }
          case 34: {
            JWToken = input.ReadString();
            break;
          }
        }
      }
    }

  }

  #endregion

}

#endregion Designer generated code


text:
  GitHub: https://github.com/ignatandrei/stankins

  Material icons list:
  https://material.io/tools/icons/?style=baseline

Live demo: https://azurestankins.azurewebsites.net

Live swagger

 Current settings

<vers-versions-netcore-angular></vers-versions-netcore-angular>


text:
require_relative "../../../../test_helper"

class Comfy::Admin::Cms::SitesControllerTest < ActionDispatch::IntegrationTest

  def test_get_index
    Comfy::Cms::Site.create!(hostname: "other.test")

    r :get, comfy_admin_cms_sites_path
    assert_response :success
    assert assigns(:sites)
    assert_template :index
  end

  def test_get_index_with_no_sites
    Comfy::Cms::Site.delete_all
    r :get, comfy_admin_cms_sites_path
    assert_response :redirect
    assert_redirected_to new_comfy_admin_cms_site_path
  end

  def test_get_new
    r :get, new_comfy_admin_cms_site_path
    assert_response :success
    assert assigns(:site)
    assert_equal "www.example.com", assigns(:site).hostname
    assert_template :new
    assert_select "form[action='/admin/sites']"
  end

  def test_get_edit
    site = comfy_cms_sites(:default)
    r :get, edit_comfy_admin_cms_site_path(id: site)
    assert_response :success
    assert assigns(:site)
    assert_template :edit
    assert_select "form[action='/admin/sites/#{site.id}']"
  end

  def test_get_edit_failure
    r :get, edit_comfy_admin_cms_site_path(id: "invalid")
    assert_response :redirect
    assert_redirected_to action: :index
    assert_equal "Site not found", flash[:danger]
  end

  def test_create
    assert_difference "Comfy::Cms::Site.count" do
      r :post, comfy_admin_cms_sites_path, params: { site: {
        label:      "Test Site",
        identifier: "test-site",
        hostname:   "test.site.local"
      } }
      assert_response :redirect
      site = Comfy::Cms::Site.last
      assert_redirected_to comfy_admin_cms_site_layouts_path(site_id: site)
      assert_equal "Site created", flash[:success]
    end
  end

  def test_creation_failure
    assert_no_difference "Comfy::Cms::Site.count" do
      r :post, comfy_admin_cms_sites_path, params: { site: {} }
      assert_response :success
      assert_template :new
      assert_equal "Failed to create site", flash[:danger]
    end
  end

  def test_update
    site = comfy_cms_sites(:default)
    r :put, comfy_admin_cms_site_path(id: site), params: { site: {
      label:    "New Site",
      hostname: "new.site.local",
      locale:   "es"
    } }
    assert_response :redirect
    assert_redirected_to action: :edit, id: site
    assert_equal "Site updated", flash[:success]
    site.reload
    assert_equal "New Site", site.label
    assert_equal "new.site.local", site.hostname
    assert_equal "es", site.locale
  end

  def test_update_failure
    site = comfy_cms_sites(:default)
    r :put, comfy_admin_cms_site_path(id: site), params: { site: {
      hostname: ""
    } }
    assert_response :success
    assert_template :edit
    site.reload
    assert_not_equal "", site.hostname
    assert_equal "Failed to update site", flash[:danger]
  end

  def test_destroy
    assert_difference "Comfy::Cms::Site.count", -1 do
      r :delete, comfy_admin_cms_site_path(id: comfy_cms_sites(:default))
      assert_response :redirect
      assert_redirected_to action: :index
      assert_equal "Site deleted", flash[:success]
    end
  end

end


text:
package org.efaps.db.search.value;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import org.efaps.admin.datamodel.IBitEnum;
import org.efaps.admin.datamodel.attributetype.BitEnumType;
import org.efaps.db.AbstractTypeQuery;
import org.efaps.db.search.AbstractQPart;
import org.efaps.db.search.compare.QEqual;
import org.efaps.db.wrapper.SQLPart;
import org.efaps.db.wrapper.SQLSelect;
import org.efaps.util.EFapsException;

/**
 * TODO comment!
 *
 * @author The eFaps Team
 *
 */
public class QBitValue
    extends AbstractQValue
{
    /**
     * BitEnum belonging to this value.
     */
    private final IBitEnum bitEnum;

    /**
     * Set of integr allready added.
     */
    private final Set<Integer> added = new HashSet<>();;

    /**
     * @param _bitEnum bit enum to be used
     */
    public QBitValue(final IBitEnum _bitEnum)
    {
        this.bitEnum = _bitEnum;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public AbstractQPart prepare(final AbstractTypeQuery _query,
                                 final AbstractQPart _part)
        throws EFapsException
    {
        if (_part instanceof QEqual) {
            final List<AbstractQValue> values = ((QEqual) _part).getValues();
            final Map<Integer, IBitEnum>enums = new TreeMap<>();
            for (final AbstractQValue value : values) {
                if (value instanceof QBitValue) {
                    final IBitEnum abitEnum = ((QBitValue) value).getBitEnum();
                    if (abitEnum.getInt() < this.bitEnum.getInt()) {
                        enums.put(abitEnum.getInt(), abitEnum);
                    }
                }
            }
            if (!enums.isEmpty()) {
                final IBitEnum aEnum = enums.values().iterator().next();
                final IBitEnum[] consts = aEnum.getClass().getEnumConstants();
                final int max = consts[consts.length - 1].getInt() * 2;
                for (int i = 0; i < max; i++) {
                    final Iterator<IBitEnum> iter = enums.values().iterator();
                    while (iter.hasNext()) {
                        final IBitEnum oEnum = iter.next();
                        if (BitEnumType.isSelected(i, oEnum)) {
                            this.added.add(i);
                        }
                    }
                }
            }
        }
        return this;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public QBitValue appendSQL(final SQLSelect _sql)
    {
        final IBitEnum[] consts = this.bitEnum.getClass().getEnumConstants();
        final int max = consts[consts.length - 1].getInt() * 2;
        boolean first = true;
        for (int i = 0; i < max; i++) {
            if (!this.added.contains(i) && BitEnumType.isSelected(i, this.bitEnum)) {
                if (first) {
                    first = false;
                } else {
                    _sql.addPart(SQLPart.COMMA);
                }
                _sql.addValuePart(i);
            }
        }
        return this;
    }

    /**
     * Getter method for the instance variable {@link #bitEnum}.
     *
     * @return value of instance variable {@link #bitEnum}
     */
    public IBitEnum getBitEnum()
    {
        return this.bitEnum;
    }
}


text:
#########################################################
# edit July 2009:
#
# argument config= sets template file
# Sebastian Stein, sebastian.stein@tu-dortmund.de
#########################################################

import os, sys, fnmatch, ConfigParser

class stuff:
	def __init__(self, ttag, tpath, tpats):
		self.tag, self.path, self.pats = ttag, tpath, tpats

def bailout(msg):
	print msg
	print 'Please use the original makeVCProj.config as a template'
	sys.exit()

def loadConfig(sect, key, noError = False ):
	if config.has_option(sect, key):
		v = config.get(sect, key)
		print '   [', sect, ']', key, '=', v
		return v
	else:
		if noError:
			return None
		else:
			bailout('Option "' + key + '" in section "' + sect + '" is missing')

#########################################################
# LOAD CONFIGURATION

config = ConfigParser.ConfigParser()

makecfg = 'makeVCProj.config'
for x in sys.argv[1:]:
    if x[:7] == "config=":
        makecfg = x[7:]

print 'Loading ' + makecfg + '...'

try:
	config.readfp( open(makecfg))
except IOError:
	bailout(makecfg + ' not found')

if not config.has_section('GENERAL'):
	bailout('Section "GENERAL" is missing')
if not config.has_section('COIN'):
	bailout('Section "COIN" is missing')

#########################################################
# CONFIGS

# Filenames
filename_vcproj =  loadConfig('GENERAL', 'projectFile')
filename_template = loadConfig('GENERAL', 'templateFile')
addIncludes = ''
addDefines = ''
addLibs = ''
addLibPathsDebugWin32 = ''
addLibPathsReleaseWin32 = ''
addLibPathsDebugX64 = ''
addLibPathsReleaseX64 = ''

useOwnLpSolver = loadConfig('GENERAL', 'useOwnLpSolver', 'false').startswith('t')
if useOwnLpSolver:
	addDefines += 'OGDF_OWN_LPSOLVER;'

useCoin = loadConfig('COIN', 'useCoin').startswith('t')
if useCoin:
	coinIncl = loadConfig('COIN', 'coinIncl')
	coinLib = loadConfig('COIN', 'coinLib')
	solver_name = loadConfig('COIN', 'solver_name')
	solver_incl = loadConfig('COIN', 'solver_incl')

	addDefines += 'USE_COIN;'+solver_name+';'
	addIncludes += coinIncl+';'
	if solver_incl.strip() != '':
		addIncludes += solver_incl+';'
	addLibs += 'libCoinUtils.lib libOsi.lib '
	addLibs += 'libClp.lib libOsiClp.lib '
	addLibPathsDebugWin32 += coinLib+'/win32/Debug;'
	addLibPathsReleaseWin32 += coinLib+'/win32/Release;'
	addLibPathsDebugX64 += coinLib+'/x64/Debug;'
	addLibPathsReleaseX64 += coinLib+'/x64/Release;'

addDefines = addDefines[:-1]
addIncludes = addIncludes[:-1]
addLibs = addLibs[:-1]
addLibPathsDebugWin32 = addLibPathsDebugWin32[:-1]
addLibPathsReleaseWin32 = addLibPathsReleaseWin32[:-1]
addLibPathsDebugX64 = addLibPathsDebugX64[:-1]
addLibPathsReleaseX64 = addLibPathsReleaseX64[:-1]
defineTag = '<<DEFINETAG>>'
includeTag = '<<INCLUDETAG>>'
libTag = '<<LIBTAG>>'
libPathsTagDebugWin32 = '<<LIBPATHSDEBUGWIN32TAG>>'
libPathsTagReleaseWin32 = '<<LIBPATHSRELEASEWIN32TAG>>'
libPathsTagDebugX64 = '<<LIBPATHSDEBUGX64TAG>>'
libPathsTagReleaseX64 = '<<LIBPATHSRELEASEX64TAG>>'

# Params are:
# - Tag in template-File
# - Directory to start search & subfilters from
# - File Patterns
cppStuff = stuff( '<<CPPTAG>>', '.\\src', [ '*.c', '*.cpp' ] )
hStuff = stuff( '<<HTAG>>', '.\\ogdf', [ '*.h' ] )
hLegacyStuff = stuff( '<<HLEGACYTAG>>', '.\\ogdf_legacy', [ '*.h' ] )
otherStuff = stuff( '<<OTHERTAG>>', '.', [ ] )

includeStuff = stuff

stuff = [ cppStuff, hStuff, hLegacyStuff, otherStuff ]

#########################################################
#########################################################
## only code below...

# just the def. nothing happens yet
def Walk( curdir, pats, intro ):
	names = os.listdir( curdir)
	names.sort()

	for name in names:
		if name.startswith('.') or name.startswith('_') or (name=='legacy' and not includeLegacyCode):
			continue

		outpath = curdir + '\\' + name
		fullname = os.path.normpath(outpath)

		if os.path.isdir(fullname) and not os.path.islink(fullname):
			if Walk( outpath, pats, intro + '<Filter Name="' + name + '">\n'):
				intro = ''
				vcproj.write('</Filter>\n')
		else:
			for pat in pats:
				if fnmatch.fnmatch(name, pat):
					if len(intro)>0:
						vcproj.write(intro)
						intro = ''
					vcproj.write('<File RelativePath="' + outpath + '"> </File>\n')
	return len(intro) == 0

##########################################
## Main below...

print 'Generating VCProj...'

includeLegacyCode = 0;
if len(sys.argv)>1 and sys.argv[1]=='legacy':
	includeLegacyCode = 1
	print '(including legacy code)'

vcproj = open(filename_vcproj,'w')
template = open(filename_template)

check = 0
for line in template:
	if check < len(stuff) and line.find(stuff[check].tag) > -1:
		if (stuff[check].tag!='<<HLEGACYTAG>>' or includeLegacyCode):
			Walk(stuff[check].path, stuff[check].pats, '')
		check = check + 1
	elif line.find(defineTag) > -1:
		vcproj.write(line.replace(defineTag,addDefines,1))
	elif line.find(includeTag) > -1:
		vcproj.write(line.replace(includeTag,addIncludes,1))
	elif line.find(libTag) > -1:
		vcproj.write(line.replace(libTag,addLibs,1))
	elif line.find(libPathsTagDebugWin32) > -1:
		vcproj.write(line.replace(libPathsTagDebugWin32,addLibPathsDebugWin32,1))
	elif line.find(libPathsTagReleaseWin32) > -1:
		vcproj.write(line.replace(libPathsTagReleaseWin32,addLibPathsReleaseWin32,1))
	elif line.find(libPathsTagDebugX64) > -1:
		vcproj.write(line.replace(libPathsTagDebugX64,addLibPathsDebugX64,1))
	elif line.find(libPathsTagReleaseX64) > -1:
		vcproj.write(line.replace(libPathsTagReleaseX64,addLibPathsReleaseX64,1))
	else:
		vcproj.write(line)

template.close()
vcproj.close()

print 'VCProj generated'


text:
@keyframes slideInRight {
	0% {
		opacity: 0;
		transform: translateX(2000px);
	}

	100% {
		transform: translateX(0);
	}
}

.slideInRight {
	animation-name: slideInRight;
}


text:
'use strict';

//Setting up route
angular.module('tcpselectionmanagers').config(['$stateProvider',
  function($stateProvider) {
    // Tcpselectionmanagers state routing
    $stateProvider.
    state('listTcpselectionmanagers', {
      url: '/tcpselectionmanagers',
      templateUrl: 'modules/tcpselectionmanagers/views/list-tcpselectionmanagers.client.view.html'
    }).
    state('createTcpselectionmanager', {
      url: '/tcpselectionmanagers/create',
      templateUrl: 'modules/tcpselectionmanagers/views/create-tcpselectionmanager.client.view.html'
    });
  }
]);


text:
package net.wastl.webmail.xml;

import java.io.CharArrayWriter;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import net.wastl.webmail.server.WebMailServer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.CDATASection;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

/**
 * A generic representation of WebMail data. Contains mainly state information
 * and the system configuration
 *
 * @author Sebastian Schaffert
 */
public class XMLGenericModel extends XMLData {
    private static Log log = LogFactory.getLog(XMLGenericModel.class);
    //protected Document root;

    protected Element sysdata;

    protected Element statedata;

    protected WebMailServer parent;

    protected long current_id=0;

    protected DocumentBuilder parser;

    public XMLGenericModel(WebMailServer parent, Element rsysdata)
        throws ParserConfigurationException, org.xml.sax.SAXException, java.io.IOException {
        super();
        this.parent=parent;

        parser=DocumentBuilderFactory.newInstance().newDocumentBuilder();

        initRoot();

        statedata=root.createElement("STATEDATA");

        NodeList nl=getNodeListXPath("//STATEDATA");

        if(nl != null && nl.getLength() > 0) {
            log.warn("Webmail usermodel template contained a STATEDATA tag, although this should only be created at runtime!");
            root.getDocumentElement().replaceChild(statedata,nl.item(0));
        } else {
            root.getDocumentElement().appendChild(statedata);
        }

        this.sysdata=rsysdata;
    }

    protected void initRoot() throws org.xml.sax.SAXException, java.io.IOException {
        // Create a new usermodel from the template file
        root = parser.parse("file://"+parent.getProperty("webmail.xml.path")+
                            System.getProperty("file.separator")+"generic_template.xml");
        this.data=root.getDocumentElement();
    }

    public Document getRoot() {
        return root;
    }

    public Element getStateData() {
        return statedata;
    }

    public void init() {
        setStateVar("base uri",parent.getBasePath());
        setStateVar("img base uri",parent.getImageBasePath()+
                    "/"+parent.getDefaultLocale().getLanguage()+
                    "/"+parent.getDefaultTheme());
        setStateVar("webmail version",parent.getVersion());
        setStateVar("operating system",System.getProperty("os.name")+" "+
                    System.getProperty("os.version")+"/"+System.getProperty("os.arch"));
        setStateVar("java virtual machine",System.getProperty("java.vendor")+" "+
                    System.getProperty("java.vm.name")+" "+System.getProperty("java.version"));
    }

    /**
     * Insert the sysdata and userdata objects into the usermodel tree
     */
    public void update() {
        Node n = null;
        try {
             n = getNodeXPath("//SYSDATA");
        } catch (TransformerException te) {
            log.error("Failed to extract node for XPath '//SYSDATA'.  "
                    + "Aborting update");
            XMLCommon.dumpXML(log, "//SYSDATA", root);
            // TODO:  throw here instead of silently failing (from
            //        user's perspective).
        }
        if (n == null) return;
        log.debug("Got a '//SYSDATA' node");
        try {
            root.getDocumentElement().replaceChild(
                    root.importNode(sysdata,true), n);
        } catch(DOMException ex) {
            log.error(
                "Failed to replace //SYSDATA Element in the XML generic model",
                ex);
            XMLCommon.dumpXML(log, "//SYSDATA", root);
            // There is a real bug that manifests here.
            // Occurs when logging into Admin Plugin (or when it brings
            // up the main System Configuration page the first time).
        }
    }

    /**
     * Create a unique ID.
     * Important: This returns a new Unique ID within this session.
     * It should be used to generate IDs for Folders and messages so that they can be easily referenced
     */
    public String getNextID() {
        return Long.toHexString(++current_id).toUpperCase();
    }

    public synchronized void setException(Exception ex) {
        Element exception=root.createElement("EXCEPTION");
        Element ex_message=root.createElement("EX_MESSAGE");
        Element ex_stacktrace=root.createElement("EX_STACKTRACE");
        exception.appendChild(ex_message);
        exception.appendChild(ex_stacktrace);

        Text msg=root.createTextNode(ex.getMessage());
        ex_message.appendChild(msg);

        String my_stack="";
        CharArrayWriter cstream=new CharArrayWriter();
        ex.printStackTrace(new PrintWriter(cstream));
        my_stack=cstream.toString();
        CDATASection stack=root.createCDATASection(my_stack);
        ex_stacktrace.appendChild(stack);

        NodeList nl=getNodeListXPath("//EXCEPTION");
        if(nl.getLength() > 0) {
            statedata.replaceChild(exception,nl.item(0));
        } else {
            statedata.appendChild(exception);
        }
        invalidateCache();

        //XMLCommon.debugXML(root);
    }

    /**
     * We need to synchronize that to avoid problems, but this should be fast anyway
     */
    public synchronized void setStateVar(String name, String value) {
        Element var= null;
        String xPathString = "//STATEDATA/VAR[@name='"+name+"']";
        //XMLCommon.getElementByAttribute(statedata,"VAR","name",name);
        try {
            var = (Element) getNodeXPath(xPathString);
        } catch (TransformerException te) {
            log.warn("'" + xPathString + "' query threw, "
                    + "but it's probably an XPath library bug.  "
                    + "Continuing as if node just not found.");
            /*
            For some reason, the XML lib is throwing when it should just
            return null, according to Xalan API spec.
            Parent node is present, but child is not...
            so it should return null.  JDom returns null.
            TODO:  Do some testing directly with the XML lib.
            log.error("Failed to get extract node for XPath '"
                    + xPathString + "'", te);
            XMLCommon.dumpXML(log, xPathString, root);
            */
        }
        if(var == null) {
            var=root.createElement("VAR");
            var.setAttribute("name",name);
            statedata.appendChild(var);
            invalidateCache();
        }
        if(!var.getAttribute("value").equals(value)) {
            var.setAttribute("value",value);
            invalidateCache();
        }
    }

    public Element createStateVar(String name, String value) {
        Element var=root.createElement("VAR");
        var.setAttribute("name",name);
        var.setAttribute("value",value);
        return var;
    }

    public void addStateVar(String name, String value) {
        Element var=root.createElement("VAR");
        var.setAttribute("name",name);
        statedata.appendChild(var);
        var.setAttribute("value",value);
        invalidateCache();
    }

    /**
     * We need to synchronize that because it can cause problems with multiple threads
     */
    public synchronized void removeAllStateVars(String name) {
        NodeList nl=getNodeListXPath("//STATEDATA/VAR");

        if(nl != null) {
            /* This suxx: NodeList Object is changed when removing children !!!
               I will store all nodes that should be deleted in a Vector and delete them afterwards */
            int length=nl.getLength();
            Vector<Element> v = new Vector<Element>(nl.getLength());
            for(int i=0;i<length;i++) {
                if(((Element)nl.item(i)).getAttribute("name").equals(name)) {
                    v.addElement((Element) nl.item(i));
                }
            }
            Enumeration enumVar=v.elements();
            while(enumVar.hasMoreElements()) {
                Node n=(Node)enumVar.nextElement();
                statedata.removeChild(n);
            }
        }
        invalidateCache();
    }

    public String getStateVar(String name) {
        Element var = null;
        String xPathString = "//STATEDATA/VAR[@name='"+name+"']";
        //XMLCommon.getElementByAttribute(statedata,"VAR","name",name);
        try {
            var = (Element) getNodeXPath(xPathString);
        } catch (TransformerException te) {
            log.error("Failed to get extract node for XPath '"
                    + xPathString + "'", te);
            XMLCommon.dumpXML(log, xPathString, root);
        }
        return (var == null) ? "" : var.getAttribute("value");
    }
}